<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"><html xmlns="http://www.w3.org/1999/xhtml"><head><link rel="stylesheet" type="text/css" href="style.css" /><script type="text/javascript" src="highlight.js"></script></head><body><pre><span class="hs-comment">{-
(c) The University of Glasgow 2006
(c) The GRASP/AQUA Project, Glasgow University, 1992-1998

-}</span><span>
</span><span id="line-6"></span><span>
</span><span id="line-7"></span><span class="hs-pragma">{-# LANGUAGE CPP #-}</span><span>
</span><span id="line-8"></span><span class="hs-pragma">{-# LANGUAGE MultiWayIf #-}</span><span>
</span><span id="line-9"></span><span>
</span><span id="line-10"></span><span class="hs-comment">-- | Functions for inferring (and simplifying) the context for derived instances.</span><span>
</span><span id="line-11"></span><span class="hs-keyword">module</span><span> </span><span class="hs-identifier">GHC.Tc.Deriv.Infer</span><span>
</span><span id="line-12"></span><span>   </span><span class="hs-special">(</span><span> </span><span class="annot"><a href="GHC.Tc.Deriv.Infer.html#inferConstraints"><span class="hs-identifier">inferConstraints</span></a></span><span>
</span><span id="line-13"></span><span>   </span><span class="hs-special">,</span><span> </span><span class="annot"><a href="GHC.Tc.Deriv.Infer.html#simplifyInstanceContexts"><span class="hs-identifier">simplifyInstanceContexts</span></a></span><span>
</span><span id="line-14"></span><span>   </span><span class="hs-special">)</span><span>
</span><span id="line-15"></span><span class="hs-keyword">where</span><span class="hs-cpp">

#include &quot;HsVersions.h&quot;
</span><span>
</span><span id="line-19"></span><span class="hs-keyword">import</span><span> </span><span class="annot"><a href="GHC.Prelude.html"><span class="hs-identifier">GHC.Prelude</span></a></span><span>
</span><span id="line-20"></span><span>
</span><span id="line-21"></span><span class="hs-keyword">import</span><span> </span><span class="annot"><a href="GHC.Data.Bag.html"><span class="hs-identifier">GHC.Data.Bag</span></a></span><span>
</span><span id="line-22"></span><span class="hs-keyword">import</span><span> </span><span class="annot"><a href="GHC.Types.Basic.html"><span class="hs-identifier">GHC.Types.Basic</span></a></span><span>
</span><span id="line-23"></span><span class="hs-keyword">import</span><span> </span><span class="annot"><a href="GHC.Core.Class.html"><span class="hs-identifier">GHC.Core.Class</span></a></span><span>
</span><span id="line-24"></span><span class="hs-keyword">import</span><span> </span><span class="annot"><a href="GHC.Core.DataCon.html"><span class="hs-identifier">GHC.Core.DataCon</span></a></span><span>
</span><span id="line-25"></span><span class="hs-keyword">import</span><span> </span><span class="annot"><a href="GHC.Utils.Error.html"><span class="hs-identifier">GHC.Utils.Error</span></a></span><span>
</span><span id="line-26"></span><span class="hs-keyword">import</span><span> </span><span class="annot"><a href="GHC.Tc.Utils.Instantiate.html"><span class="hs-identifier">GHC.Tc.Utils.Instantiate</span></a></span><span>
</span><span id="line-27"></span><span class="hs-keyword">import</span><span> </span><span class="annot"><a href="GHC.Utils.Outputable.html"><span class="hs-identifier">GHC.Utils.Outputable</span></a></span><span>
</span><span id="line-28"></span><span class="hs-keyword">import</span><span> </span><span class="annot"><a href="GHC.Data.Pair.html"><span class="hs-identifier">GHC.Data.Pair</span></a></span><span>
</span><span id="line-29"></span><span class="hs-keyword">import</span><span> </span><span class="annot"><a href="GHC.Builtin.Names.html"><span class="hs-identifier">GHC.Builtin.Names</span></a></span><span>
</span><span id="line-30"></span><span class="hs-keyword">import</span><span> </span><span class="annot"><a href="GHC.Tc.Deriv.Utils.html"><span class="hs-identifier">GHC.Tc.Deriv.Utils</span></a></span><span>
</span><span id="line-31"></span><span class="hs-keyword">import</span><span> </span><span class="annot"><a href="GHC.Tc.Utils.Env.html"><span class="hs-identifier">GHC.Tc.Utils.Env</span></a></span><span>
</span><span id="line-32"></span><span class="hs-keyword">import</span><span> </span><span class="annot"><a href="GHC.Tc.Deriv.Generate.html"><span class="hs-identifier">GHC.Tc.Deriv.Generate</span></a></span><span>
</span><span id="line-33"></span><span class="hs-keyword">import</span><span> </span><span class="annot"><a href="GHC.Tc.Deriv.Functor.html"><span class="hs-identifier">GHC.Tc.Deriv.Functor</span></a></span><span>
</span><span id="line-34"></span><span class="hs-keyword">import</span><span> </span><span class="annot"><a href="GHC.Tc.Deriv.Generics.html"><span class="hs-identifier">GHC.Tc.Deriv.Generics</span></a></span><span>
</span><span id="line-35"></span><span class="hs-keyword">import</span><span> </span><span class="annot"><a href="GHC.Tc.Utils.TcMType.html"><span class="hs-identifier">GHC.Tc.Utils.TcMType</span></a></span><span>
</span><span id="line-36"></span><span class="hs-keyword">import</span><span> </span><span class="annot"><a href="GHC.Tc.Utils.Monad.html"><span class="hs-identifier">GHC.Tc.Utils.Monad</span></a></span><span>
</span><span id="line-37"></span><span class="hs-keyword">import</span><span> </span><span class="annot"><a href="GHC.Tc.Types.Origin.html"><span class="hs-identifier">GHC.Tc.Types.Origin</span></a></span><span>
</span><span id="line-38"></span><span class="hs-keyword">import</span><span> </span><span class="annot"><a href="GHC.Tc.Types.Constraint.html"><span class="hs-identifier">GHC.Tc.Types.Constraint</span></a></span><span>
</span><span id="line-39"></span><span class="hs-keyword">import</span><span> </span><span class="annot"><a href="GHC.Core.Predicate.html"><span class="hs-identifier">GHC.Core.Predicate</span></a></span><span>
</span><span id="line-40"></span><span class="hs-keyword">import</span><span> </span><span class="annot"><a href="GHC.Tc.Utils.TcType.html"><span class="hs-identifier">GHC.Tc.Utils.TcType</span></a></span><span>
</span><span id="line-41"></span><span class="hs-keyword">import</span><span> </span><span class="annot"><a href="GHC.Core.TyCon.html"><span class="hs-identifier">GHC.Core.TyCon</span></a></span><span>
</span><span id="line-42"></span><span class="hs-keyword">import</span><span> </span><span class="annot"><a href="GHC.Core.TyCo.Ppr.html"><span class="hs-identifier">GHC.Core.TyCo.Ppr</span></a></span><span> </span><span class="hs-special">(</span><span class="annot"><a href="GHC.Core.TyCo.Ppr.html#pprTyVars"><span class="hs-identifier">pprTyVars</span></a></span><span class="hs-special">)</span><span>
</span><span id="line-43"></span><span class="hs-keyword">import</span><span> </span><span class="annot"><a href="GHC.Core.Type.html"><span class="hs-identifier">GHC.Core.Type</span></a></span><span>
</span><span id="line-44"></span><span class="hs-keyword">import</span><span> </span><span class="annot"><a href="GHC.Tc.Solver.html"><span class="hs-identifier">GHC.Tc.Solver</span></a></span><span>
</span><span id="line-45"></span><span class="hs-keyword">import</span><span> </span><span class="annot"><a href="GHC.Tc.Validity.html"><span class="hs-identifier">GHC.Tc.Validity</span></a></span><span> </span><span class="hs-special">(</span><span class="annot"><a href="GHC.Tc.Validity.html#validDerivPred"><span class="hs-identifier">validDerivPred</span></a></span><span class="hs-special">)</span><span>
</span><span id="line-46"></span><span class="hs-keyword">import</span><span> </span><span class="annot"><a href="GHC.Tc.Utils.Unify.html"><span class="hs-identifier">GHC.Tc.Utils.Unify</span></a></span><span> </span><span class="hs-special">(</span><span class="annot"><a href="GHC.Tc.Utils.Unify.html#buildImplicationFor"><span class="hs-identifier">buildImplicationFor</span></a></span><span class="hs-special">,</span><span> </span><span class="annot"><a href="GHC.Tc.Utils.Unify.html#checkConstraints"><span class="hs-identifier">checkConstraints</span></a></span><span class="hs-special">)</span><span>
</span><span id="line-47"></span><span class="hs-keyword">import</span><span> </span><span class="annot"><a href="GHC.Builtin.Types.html"><span class="hs-identifier">GHC.Builtin.Types</span></a></span><span> </span><span class="hs-special">(</span><span class="annot"><a href="GHC.Builtin.Types.html#typeToTypeKind"><span class="hs-identifier">typeToTypeKind</span></a></span><span class="hs-special">)</span><span>
</span><span id="line-48"></span><span class="hs-keyword">import</span><span> </span><span class="annot"><a href="GHC.Core.Unify.html"><span class="hs-identifier">GHC.Core.Unify</span></a></span><span> </span><span class="hs-special">(</span><span class="annot"><a href="GHC.Core.Unify.html#tcUnifyTy"><span class="hs-identifier">tcUnifyTy</span></a></span><span class="hs-special">)</span><span>
</span><span id="line-49"></span><span class="hs-keyword">import</span><span> </span><span class="annot"><a href="GHC.Utils.Misc.html"><span class="hs-identifier">GHC.Utils.Misc</span></a></span><span>
</span><span id="line-50"></span><span class="hs-keyword">import</span><span> </span><span class="annot"><a href="GHC.Types.Var.html"><span class="hs-identifier">GHC.Types.Var</span></a></span><span>
</span><span id="line-51"></span><span class="hs-keyword">import</span><span> </span><span class="annot"><a href="GHC.Types.Var.Set.html"><span class="hs-identifier">GHC.Types.Var.Set</span></a></span><span>
</span><span id="line-52"></span><span>
</span><span id="line-53"></span><span class="hs-keyword">import</span><span> </span><span class="annot"><a href="../../base/src/Control.Monad.html#"><span class="hs-identifier">Control.Monad</span></a></span><span>
</span><span id="line-54"></span><span class="hs-keyword">import</span><span> </span><span class="annot"><a href="../../transformers/src/Control.Monad.Trans.Class.html#"><span class="hs-identifier">Control.Monad.Trans.Class</span></a></span><span>  </span><span class="hs-special">(</span><span class="annot"><a href="../../transformers/src/Control.Monad.Trans.Class.html#lift"><span class="hs-identifier">lift</span></a></span><span class="hs-special">)</span><span>
</span><span id="line-55"></span><span class="hs-keyword">import</span><span> </span><span class="annot"><a href="../../transformers/src/Control.Monad.Trans.Reader.html#"><span class="hs-identifier">Control.Monad.Trans.Reader</span></a></span><span> </span><span class="hs-special">(</span><span class="annot"><a href="../../transformers/src/Control.Monad.Trans.Reader.html#ask"><span class="hs-identifier">ask</span></a></span><span class="hs-special">)</span><span>
</span><span id="line-56"></span><span class="hs-keyword">import</span><span> </span><span class="annot"><a href="../../base/src/Data.List.html#"><span class="hs-identifier">Data.List</span></a></span><span>                  </span><span class="hs-special">(</span><span class="annot"><a href="../../base/src/Data.OldList.html#sortBy"><span class="hs-identifier">sortBy</span></a></span><span class="hs-special">)</span><span>
</span><span id="line-57"></span><span class="hs-keyword">import</span><span> </span><span class="annot"><a href="../../base/src/Data.Maybe.html#"><span class="hs-identifier">Data.Maybe</span></a></span><span>
</span><span id="line-58"></span><span>
</span><span id="line-59"></span><span class="hs-comment">----------------------</span><span>
</span><span id="line-60"></span><span>
</span><span id="line-61"></span><span class="annot"><a href="GHC.Tc.Deriv.Infer.html#inferConstraints"><span class="hs-identifier hs-type">inferConstraints</span></a></span><span> </span><span class="hs-glyph">::</span><span> </span><span class="annot"><a href="GHC.Tc.Deriv.Utils.html#DerivSpecMechanism"><span class="hs-identifier hs-type">DerivSpecMechanism</span></a></span><span>
</span><span id="line-62"></span><span>                 </span><span class="hs-glyph">-&gt;</span><span> </span><span class="annot"><a href="GHC.Tc.Deriv.Utils.html#DerivM"><span class="hs-identifier hs-type">DerivM</span></a></span><span> </span><span class="hs-special">(</span><span class="hs-special">[</span><span class="annot"><a href="GHC.Tc.Deriv.Utils.html#ThetaOrigin"><span class="hs-identifier hs-type">ThetaOrigin</span></a></span><span class="hs-special">]</span><span class="hs-special">,</span><span> </span><span class="hs-special">[</span><span class="annot"><a href="GHC.Types.Var.html#TyVar"><span class="hs-identifier hs-type">TyVar</span></a></span><span class="hs-special">]</span><span class="hs-special">,</span><span> </span><span class="hs-special">[</span><span class="annot"><a href="GHC.Tc.Utils.TcType.html#TcType"><span class="hs-identifier hs-type">TcType</span></a></span><span class="hs-special">]</span><span class="hs-special">)</span><span>
</span><span id="line-63"></span><span class="hs-comment">-- inferConstraints figures out the constraints needed for the</span><span>
</span><span id="line-64"></span><span class="hs-comment">-- instance declaration generated by a 'deriving' clause on a</span><span>
</span><span id="line-65"></span><span class="hs-comment">-- data type declaration. It also returns the new in-scope type</span><span>
</span><span id="line-66"></span><span class="hs-comment">-- variables and instance types, in case they were changed due to</span><span>
</span><span id="line-67"></span><span class="hs-comment">-- the presence of functor-like constraints.</span><span>
</span><span id="line-68"></span><span class="hs-comment">-- See Note [Inferring the instance context]</span><span>
</span><span id="line-69"></span><span>
</span><span id="line-70"></span><span class="hs-comment">-- e.g. inferConstraints</span><span>
</span><span id="line-71"></span><span class="hs-comment">--        C Int (T [a])    -- Class and inst_tys</span><span>
</span><span id="line-72"></span><span class="hs-comment">--        :RTList a        -- Rep tycon and its arg tys</span><span>
</span><span id="line-73"></span><span class="hs-comment">-- where T [a] ~R :RTList a</span><span>
</span><span id="line-74"></span><span class="hs-comment">--</span><span>
</span><span id="line-75"></span><span class="hs-comment">-- Generate a sufficiently large set of constraints that typechecking the</span><span>
</span><span id="line-76"></span><span class="hs-comment">-- generated method definitions should succeed.   This set will be simplified</span><span>
</span><span id="line-77"></span><span class="hs-comment">-- before being used in the instance declaration</span><span>
</span><span id="line-78"></span><span id="inferConstraints"><span class="annot"><span class="annottext">inferConstraints :: DerivSpecMechanism -&gt; DerivM ([ThetaOrigin], [TcTyVar], ThetaType)
</span><a href="GHC.Tc.Deriv.Infer.html#inferConstraints"><span class="hs-identifier hs-var hs-var">inferConstraints</span></a></span></span><span> </span><span id="local-6989586621681174537"><span class="annot"><span class="annottext">DerivSpecMechanism
</span><a href="#local-6989586621681174537"><span class="hs-identifier hs-var">mechanism</span></a></span></span><span>
</span><span id="line-79"></span><span>  </span><span class="hs-glyph">=</span><span> </span><span class="hs-keyword">do</span><span> </span><span class="hs-special">{</span><span> </span><span class="annot"><a href="GHC.Tc.Deriv.Utils.html#DerivEnv"><span class="hs-identifier hs-type">DerivEnv</span></a></span><span> </span><span class="hs-special">{</span><span> </span><span class="annot"><span class="annottext">denv_tvs :: DerivEnv -&gt; [TcTyVar]
</span><a href="GHC.Tc.Deriv.Utils.html#denv_tvs"><span class="hs-identifier hs-var">denv_tvs</span></a></span><span>      </span><span class="hs-glyph">=</span><span> </span><span id="local-6989586621681174534"><span class="annot"><span class="annottext">[TcTyVar]
</span><a href="#local-6989586621681174534"><span class="hs-identifier hs-var">tvs</span></a></span></span><span>
</span><span id="line-80"></span><span>                  </span><span class="hs-special">,</span><span> </span><span class="annot"><span class="annottext">denv_cls :: DerivEnv -&gt; Class
</span><a href="GHC.Tc.Deriv.Utils.html#denv_cls"><span class="hs-identifier hs-var">denv_cls</span></a></span><span>      </span><span class="hs-glyph">=</span><span> </span><span id="local-6989586621681174532"><span class="annot"><span class="annottext">Class
</span><a href="#local-6989586621681174532"><span class="hs-identifier hs-var">main_cls</span></a></span></span><span>
</span><span id="line-81"></span><span>                  </span><span class="hs-special">,</span><span> </span><span class="annot"><span class="annottext">denv_inst_tys :: DerivEnv -&gt; ThetaType
</span><a href="GHC.Tc.Deriv.Utils.html#denv_inst_tys"><span class="hs-identifier hs-var">denv_inst_tys</span></a></span><span> </span><span class="hs-glyph">=</span><span> </span><span id="local-6989586621681174530"><span class="annot"><span class="annottext">ThetaType
</span><a href="#local-6989586621681174530"><span class="hs-identifier hs-var">inst_tys</span></a></span></span><span> </span><span class="hs-special">}</span><span> </span><span class="hs-glyph">&lt;-</span><span> </span><span class="annot"><span class="annottext">ReaderT DerivEnv (IOEnv (Env TcGblEnv TcLclEnv)) DerivEnv
forall (m :: * -&gt; *) r. Monad m =&gt; ReaderT r m r
</span><a href="../../transformers/src/Control.Monad.Trans.Reader.html#ask"><span class="hs-identifier hs-var">ask</span></a></span><span>
</span><span id="line-82"></span><span>       </span><span class="hs-special">;</span><span> </span><span id="local-6989586621681174529"><span class="annot"><span class="annottext">Bool
</span><a href="#local-6989586621681174529"><span class="hs-identifier hs-var">wildcard</span></a></span></span><span> </span><span class="hs-glyph">&lt;-</span><span> </span><span class="annot"><span class="annottext">DerivM Bool
</span><a href="GHC.Tc.Deriv.Utils.html#isStandaloneWildcardDeriv"><span class="hs-identifier hs-var">isStandaloneWildcardDeriv</span></a></span><span>
</span><span id="line-83"></span><span>       </span><span class="hs-special">;</span><span> </span><span class="hs-keyword">let</span><span> </span><span class="annot"><a href="#local-6989586621681174527"><span class="hs-identifier hs-type">infer_constraints</span></a></span><span> </span><span class="hs-glyph">::</span><span> </span><span class="annot"><a href="GHC.Tc.Deriv.Utils.html#DerivM"><span class="hs-identifier hs-type">DerivM</span></a></span><span> </span><span class="hs-special">(</span><span class="hs-special">[</span><span class="annot"><a href="GHC.Tc.Deriv.Utils.html#ThetaOrigin"><span class="hs-identifier hs-type">ThetaOrigin</span></a></span><span class="hs-special">]</span><span class="hs-special">,</span><span> </span><span class="hs-special">[</span><span class="annot"><a href="GHC.Types.Var.html#TyVar"><span class="hs-identifier hs-type">TyVar</span></a></span><span class="hs-special">]</span><span class="hs-special">,</span><span> </span><span class="hs-special">[</span><span class="annot"><a href="GHC.Tc.Utils.TcType.html#TcType"><span class="hs-identifier hs-type">TcType</span></a></span><span class="hs-special">]</span><span class="hs-special">)</span><span>
</span><span id="line-84"></span><span>             </span><span id="local-6989586621681174527"><span class="annot"><span class="annottext">infer_constraints :: DerivM ([ThetaOrigin], [TcTyVar], ThetaType)
</span><a href="#local-6989586621681174527"><span class="hs-identifier hs-var hs-var">infer_constraints</span></a></span></span><span> </span><span class="hs-glyph">=</span><span>
</span><span id="line-85"></span><span>               </span><span class="hs-keyword">case</span><span> </span><span class="annot"><span class="annottext">DerivSpecMechanism
</span><a href="#local-6989586621681174537"><span class="hs-identifier hs-var">mechanism</span></a></span><span> </span><span class="hs-keyword">of</span><span>
</span><span id="line-86"></span><span>                 </span><span class="annot"><a href="GHC.Tc.Deriv.Utils.html#DerivSpecStock"><span class="hs-identifier hs-type">DerivSpecStock</span></a></span><span class="hs-special">{</span><span class="annot"><span class="annottext">dsm_stock_dit :: DerivSpecMechanism -&gt; DerivInstTys
</span><a href="GHC.Tc.Deriv.Utils.html#dsm_stock_dit"><span class="hs-identifier hs-var">dsm_stock_dit</span></a></span><span> </span><span class="hs-glyph">=</span><span> </span><span id="local-6989586621681174524"><span class="annot"><span class="annottext">DerivInstTys
</span><a href="#local-6989586621681174524"><span class="hs-identifier hs-var">dit</span></a></span></span><span class="hs-special">}</span><span>
</span><span id="line-87"></span><span>                   </span><span class="hs-glyph">-&gt;</span><span> </span><span class="annot"><span class="annottext">DerivInstTys -&gt; DerivM ([ThetaOrigin], [TcTyVar], ThetaType)
</span><a href="GHC.Tc.Deriv.Infer.html#inferConstraintsStock"><span class="hs-identifier hs-var">inferConstraintsStock</span></a></span><span> </span><span class="annot"><span class="annottext">DerivInstTys
</span><a href="#local-6989586621681174524"><span class="hs-identifier hs-var">dit</span></a></span><span>
</span><span id="line-88"></span><span>                 </span><span class="annot"><span class="annottext">DerivSpecMechanism
</span><a href="GHC.Tc.Deriv.Utils.html#DerivSpecAnyClass"><span class="hs-identifier hs-var">DerivSpecAnyClass</span></a></span><span>
</span><span id="line-89"></span><span>                   </span><span class="hs-glyph">-&gt;</span><span> </span><span class="annot"><span class="annottext">DerivM [ThetaOrigin]
-&gt; DerivM ([ThetaOrigin], [TcTyVar], ThetaType)
</span><a href="#local-6989586621681174521"><span class="hs-identifier hs-var">infer_constraints_simple</span></a></span><span> </span><span class="annot"><span class="annottext">DerivM [ThetaOrigin]
</span><a href="GHC.Tc.Deriv.Infer.html#inferConstraintsAnyclass"><span class="hs-identifier hs-var">inferConstraintsAnyclass</span></a></span><span>
</span><span id="line-90"></span><span>                 </span><span class="annot"><a href="GHC.Tc.Deriv.Utils.html#DerivSpecNewtype"><span class="hs-identifier hs-type">DerivSpecNewtype</span></a></span><span> </span><span class="hs-special">{</span><span> </span><span class="annot"><span class="annottext">dsm_newtype_dit :: DerivSpecMechanism -&gt; DerivInstTys
</span><a href="GHC.Tc.Deriv.Utils.html#dsm_newtype_dit"><span class="hs-identifier hs-var">dsm_newtype_dit</span></a></span><span> </span><span class="hs-glyph">=</span><span>
</span><span id="line-91"></span><span>                                      </span><span class="annot"><a href="GHC.Tc.Deriv.Utils.html#DerivInstTys"><span class="hs-identifier hs-type">DerivInstTys</span></a></span><span class="hs-special">{</span><span class="annot"><span class="annottext">dit_cls_tys :: DerivInstTys -&gt; ThetaType
</span><a href="GHC.Tc.Deriv.Utils.html#dit_cls_tys"><span class="hs-identifier hs-var">dit_cls_tys</span></a></span><span> </span><span class="hs-glyph">=</span><span> </span><span id="local-6989586621681174515"><span class="annot"><span class="annottext">ThetaType
</span><a href="#local-6989586621681174515"><span class="hs-identifier hs-var">cls_tys</span></a></span></span><span class="hs-special">}</span><span>
</span><span id="line-92"></span><span>                                  </span><span class="hs-special">,</span><span> </span><span class="annot"><span class="annottext">dsm_newtype_rep_ty :: DerivSpecMechanism -&gt; PredType
</span><a href="GHC.Tc.Deriv.Utils.html#dsm_newtype_rep_ty"><span class="hs-identifier hs-var">dsm_newtype_rep_ty</span></a></span><span> </span><span class="hs-glyph">=</span><span> </span><span id="local-6989586621681174513"><span class="annot"><span class="annottext">PredType
</span><a href="#local-6989586621681174513"><span class="hs-identifier hs-var">rep_ty</span></a></span></span><span> </span><span class="hs-special">}</span><span>
</span><span id="line-93"></span><span>                   </span><span class="hs-glyph">-&gt;</span><span> </span><span class="annot"><span class="annottext">DerivM [ThetaOrigin]
-&gt; DerivM ([ThetaOrigin], [TcTyVar], ThetaType)
</span><a href="#local-6989586621681174521"><span class="hs-identifier hs-var">infer_constraints_simple</span></a></span><span> </span><span class="annot"><span class="annottext">(DerivM [ThetaOrigin]
 -&gt; DerivM ([ThetaOrigin], [TcTyVar], ThetaType))
-&gt; DerivM [ThetaOrigin]
-&gt; DerivM ([ThetaOrigin], [TcTyVar], ThetaType)
forall a b. (a -&gt; b) -&gt; a -&gt; b
</span><a href="../../base/src/GHC.Base.html#%24"><span class="hs-operator hs-var">$</span></a></span><span>
</span><span id="line-94"></span><span>                      </span><span class="annot"><span class="annottext">ThetaType -&gt; PredType -&gt; DerivM [ThetaOrigin]
</span><a href="GHC.Tc.Deriv.Infer.html#inferConstraintsCoerceBased"><span class="hs-identifier hs-var">inferConstraintsCoerceBased</span></a></span><span> </span><span class="annot"><span class="annottext">ThetaType
</span><a href="#local-6989586621681174515"><span class="hs-identifier hs-var">cls_tys</span></a></span><span> </span><span class="annot"><span class="annottext">PredType
</span><a href="#local-6989586621681174513"><span class="hs-identifier hs-var">rep_ty</span></a></span><span>
</span><span id="line-95"></span><span>                 </span><span class="annot"><a href="GHC.Tc.Deriv.Utils.html#DerivSpecVia"><span class="hs-identifier hs-type">DerivSpecVia</span></a></span><span> </span><span class="hs-special">{</span><span> </span><span class="annot"><span class="annottext">dsm_via_cls_tys :: DerivSpecMechanism -&gt; ThetaType
</span><a href="GHC.Tc.Deriv.Utils.html#dsm_via_cls_tys"><span class="hs-identifier hs-var">dsm_via_cls_tys</span></a></span><span> </span><span class="hs-glyph">=</span><span> </span><span id="local-6989586621681174509"><span class="annot"><span class="annottext">ThetaType
</span><a href="#local-6989586621681174509"><span class="hs-identifier hs-var">cls_tys</span></a></span></span><span>
</span><span id="line-96"></span><span>                              </span><span class="hs-special">,</span><span> </span><span class="annot"><span class="annottext">dsm_via_ty :: DerivSpecMechanism -&gt; PredType
</span><a href="GHC.Tc.Deriv.Utils.html#dsm_via_ty"><span class="hs-identifier hs-var">dsm_via_ty</span></a></span><span> </span><span class="hs-glyph">=</span><span> </span><span id="local-6989586621681174507"><span class="annot"><span class="annottext">PredType
</span><a href="#local-6989586621681174507"><span class="hs-identifier hs-var">via_ty</span></a></span></span><span> </span><span class="hs-special">}</span><span>
</span><span id="line-97"></span><span>                   </span><span class="hs-glyph">-&gt;</span><span> </span><span class="annot"><span class="annottext">DerivM [ThetaOrigin]
-&gt; DerivM ([ThetaOrigin], [TcTyVar], ThetaType)
</span><a href="#local-6989586621681174521"><span class="hs-identifier hs-var">infer_constraints_simple</span></a></span><span> </span><span class="annot"><span class="annottext">(DerivM [ThetaOrigin]
 -&gt; DerivM ([ThetaOrigin], [TcTyVar], ThetaType))
-&gt; DerivM [ThetaOrigin]
-&gt; DerivM ([ThetaOrigin], [TcTyVar], ThetaType)
forall a b. (a -&gt; b) -&gt; a -&gt; b
</span><a href="../../base/src/GHC.Base.html#%24"><span class="hs-operator hs-var">$</span></a></span><span>
</span><span id="line-98"></span><span>                      </span><span class="annot"><span class="annottext">ThetaType -&gt; PredType -&gt; DerivM [ThetaOrigin]
</span><a href="GHC.Tc.Deriv.Infer.html#inferConstraintsCoerceBased"><span class="hs-identifier hs-var">inferConstraintsCoerceBased</span></a></span><span> </span><span class="annot"><span class="annottext">ThetaType
</span><a href="#local-6989586621681174509"><span class="hs-identifier hs-var">cls_tys</span></a></span><span> </span><span class="annot"><span class="annottext">PredType
</span><a href="#local-6989586621681174507"><span class="hs-identifier hs-var">via_ty</span></a></span><span>
</span><span id="line-99"></span><span>
</span><span id="line-100"></span><span>             </span><span class="hs-comment">-- Most deriving strategies do not need to do anything special to</span><span>
</span><span id="line-101"></span><span>             </span><span class="hs-comment">-- the type variables and arguments to the class in the derived</span><span>
</span><span id="line-102"></span><span>             </span><span class="hs-comment">-- instance, so they can pass through unchanged. The exception to</span><span>
</span><span id="line-103"></span><span>             </span><span class="hs-comment">-- this rule is stock deriving. See</span><span>
</span><span id="line-104"></span><span>             </span><span class="hs-comment">-- Note [Inferring the instance context].</span><span>
</span><span id="line-105"></span><span>             </span><span class="annot"><a href="#local-6989586621681174521"><span class="hs-identifier hs-type">infer_constraints_simple</span></a></span><span>
</span><span id="line-106"></span><span>               </span><span class="hs-glyph">::</span><span> </span><span class="annot"><a href="GHC.Tc.Deriv.Utils.html#DerivM"><span class="hs-identifier hs-type">DerivM</span></a></span><span> </span><span class="hs-special">[</span><span class="annot"><a href="GHC.Tc.Deriv.Utils.html#ThetaOrigin"><span class="hs-identifier hs-type">ThetaOrigin</span></a></span><span class="hs-special">]</span><span>
</span><span id="line-107"></span><span>               </span><span class="hs-glyph">-&gt;</span><span> </span><span class="annot"><a href="GHC.Tc.Deriv.Utils.html#DerivM"><span class="hs-identifier hs-type">DerivM</span></a></span><span> </span><span class="hs-special">(</span><span class="hs-special">[</span><span class="annot"><a href="GHC.Tc.Deriv.Utils.html#ThetaOrigin"><span class="hs-identifier hs-type">ThetaOrigin</span></a></span><span class="hs-special">]</span><span class="hs-special">,</span><span> </span><span class="hs-special">[</span><span class="annot"><a href="GHC.Types.Var.html#TyVar"><span class="hs-identifier hs-type">TyVar</span></a></span><span class="hs-special">]</span><span class="hs-special">,</span><span> </span><span class="hs-special">[</span><span class="annot"><a href="GHC.Tc.Utils.TcType.html#TcType"><span class="hs-identifier hs-type">TcType</span></a></span><span class="hs-special">]</span><span class="hs-special">)</span><span>
</span><span id="line-108"></span><span>             </span><span id="local-6989586621681174521"><span class="annot"><span class="annottext">infer_constraints_simple :: DerivM [ThetaOrigin]
-&gt; DerivM ([ThetaOrigin], [TcTyVar], ThetaType)
</span><a href="#local-6989586621681174521"><span class="hs-identifier hs-var hs-var">infer_constraints_simple</span></a></span></span><span> </span><span id="local-6989586621681174506"><span class="annot"><span class="annottext">DerivM [ThetaOrigin]
</span><a href="#local-6989586621681174506"><span class="hs-identifier hs-var">infer_thetas</span></a></span></span><span> </span><span class="hs-glyph">=</span><span> </span><span class="hs-keyword">do</span><span>
</span><span id="line-109"></span><span>               </span><span id="local-6989586621681174505"><span class="annot"><span class="annottext">[ThetaOrigin]
</span><a href="#local-6989586621681174505"><span class="hs-identifier hs-var">thetas</span></a></span></span><span> </span><span class="hs-glyph">&lt;-</span><span> </span><span class="annot"><span class="annottext">DerivM [ThetaOrigin]
</span><a href="#local-6989586621681174506"><span class="hs-identifier hs-var">infer_thetas</span></a></span><span>
</span><span id="line-110"></span><span>               </span><span class="annot"><span class="annottext">([ThetaOrigin], [TcTyVar], ThetaType)
-&gt; DerivM ([ThetaOrigin], [TcTyVar], ThetaType)
forall (f :: * -&gt; *) a. Applicative f =&gt; a -&gt; f a
</span><a href="../../base/src/GHC.Base.html#pure"><span class="hs-identifier hs-var">pure</span></a></span><span> </span><span class="hs-special">(</span><span class="annot"><span class="annottext">[ThetaOrigin]
</span><a href="#local-6989586621681174505"><span class="hs-identifier hs-var">thetas</span></a></span><span class="hs-special">,</span><span> </span><span class="annot"><span class="annottext">[TcTyVar]
</span><a href="#local-6989586621681174534"><span class="hs-identifier hs-var">tvs</span></a></span><span class="hs-special">,</span><span> </span><span class="annot"><span class="annottext">ThetaType
</span><a href="#local-6989586621681174530"><span class="hs-identifier hs-var">inst_tys</span></a></span><span class="hs-special">)</span><span>
</span><span id="line-111"></span><span>
</span><span id="line-112"></span><span>             </span><span class="hs-comment">-- Constraints arising from superclasses</span><span>
</span><span id="line-113"></span><span>             </span><span class="hs-comment">-- See Note [Superclasses of derived instance]</span><span>
</span><span id="line-114"></span><span>             </span><span id="local-6989586621681174504"><span class="annot"><span class="annottext">cls_tvs :: [TcTyVar]
</span><a href="#local-6989586621681174504"><span class="hs-identifier hs-var hs-var">cls_tvs</span></a></span></span><span>  </span><span class="hs-glyph">=</span><span> </span><span class="annot"><span class="annottext">Class -&gt; [TcTyVar]
</span><a href="GHC.Core.Class.html#classTyVars"><span class="hs-identifier hs-var hs-var">classTyVars</span></a></span><span> </span><span class="annot"><span class="annottext">Class
</span><a href="#local-6989586621681174532"><span class="hs-identifier hs-var">main_cls</span></a></span><span>
</span><span id="line-115"></span><span>             </span><span id="local-6989586621681174495"><span class="annot"><span class="annottext">sc_constraints :: [ThetaOrigin]
</span><a href="#local-6989586621681174495"><span class="hs-identifier hs-var hs-var">sc_constraints</span></a></span></span><span> </span><span class="hs-glyph">=</span><span> </span><span class="hs-identifier">ASSERT2</span><span class="hs-special">(</span><span> </span><span class="hs-identifier">equalLength</span><span> </span><span class="hs-identifier">cls_tvs</span><span> </span><span class="hs-identifier">inst_tys</span><span>
</span><span id="line-116"></span><span>                                     </span><span class="hs-special">,</span><span> </span><span class="hs-identifier">ppr</span><span> </span><span class="hs-identifier">main_cls</span><span> </span><span class="hs-operator">&lt;+&gt;</span><span> </span><span class="hs-identifier">ppr</span><span> </span><span class="hs-identifier">inst_tys</span><span> </span><span class="hs-special">)</span><span>
</span><span id="line-117"></span><span>                              </span><span class="hs-special">[</span><span> </span><span class="annot"><span class="annottext">CtOrigin
-&gt; TypeOrKind
-&gt; [TcTyVar]
-&gt; [TcTyVar]
-&gt; ThetaType
-&gt; ThetaType
-&gt; ThetaOrigin
</span><a href="GHC.Tc.Deriv.Utils.html#mkThetaOrigin"><span class="hs-identifier hs-var">mkThetaOrigin</span></a></span><span> </span><span class="hs-special">(</span><span class="annot"><span class="annottext">Bool -&gt; CtOrigin
</span><a href="GHC.Tc.Deriv.Utils.html#mkDerivOrigin"><span class="hs-identifier hs-var">mkDerivOrigin</span></a></span><span> </span><span class="annot"><span class="annottext">Bool
</span><a href="#local-6989586621681174529"><span class="hs-identifier hs-var">wildcard</span></a></span><span class="hs-special">)</span><span>
</span><span id="line-118"></span><span>                                              </span><span class="annot"><span class="annottext">TypeOrKind
</span><a href="GHC.Types.Basic.html#TypeLevel"><span class="hs-identifier hs-var">TypeLevel</span></a></span><span> </span><span class="hs-special">[</span><span class="hs-special">]</span><span> </span><span class="hs-special">[</span><span class="hs-special">]</span><span> </span><span class="hs-special">[</span><span class="hs-special">]</span><span> </span><span class="annot"><span class="annottext">(ThetaType -&gt; ThetaOrigin) -&gt; ThetaType -&gt; ThetaOrigin
forall a b. (a -&gt; b) -&gt; a -&gt; b
</span><a href="../../base/src/GHC.Base.html#%24"><span class="hs-operator hs-var">$</span></a></span><span>
</span><span id="line-119"></span><span>                                </span><span class="annot"><span class="annottext">HasCallStack =&gt; TCvSubst -&gt; ThetaType -&gt; ThetaType
TCvSubst -&gt; ThetaType -&gt; ThetaType
</span><a href="GHC.Core.TyCo.Subst.html#substTheta"><span class="hs-identifier hs-var">substTheta</span></a></span><span> </span><span class="annot"><span class="annottext">TCvSubst
</span><a href="#local-6989586621681174483"><span class="hs-identifier hs-var">cls_subst</span></a></span><span> </span><span class="hs-special">(</span><span class="annot"><span class="annottext">Class -&gt; ThetaType
</span><a href="GHC.Core.Class.html#classSCTheta"><span class="hs-identifier hs-var">classSCTheta</span></a></span><span> </span><span class="annot"><span class="annottext">Class
</span><a href="#local-6989586621681174532"><span class="hs-identifier hs-var">main_cls</span></a></span><span class="hs-special">)</span><span> </span><span class="hs-special">]</span><span>
</span><span id="line-120"></span><span>             </span><span id="local-6989586621681174483"><span class="annot"><span class="annottext">cls_subst :: TCvSubst
</span><a href="#local-6989586621681174483"><span class="hs-identifier hs-var hs-var">cls_subst</span></a></span></span><span> </span><span class="hs-glyph">=</span><span> </span><span class="hs-identifier">ASSERT</span><span class="hs-special">(</span><span> </span><span class="hs-identifier">equalLength</span><span> </span><span class="hs-identifier">cls_tvs</span><span> </span><span class="hs-identifier">inst_tys</span><span> </span><span class="hs-special">)</span><span>
</span><span id="line-121"></span><span>                         </span><span class="annot"><span class="annottext">[TcTyVar] -&gt; ThetaType -&gt; TCvSubst
HasDebugCallStack =&gt; [TcTyVar] -&gt; ThetaType -&gt; TCvSubst
</span><a href="GHC.Core.TyCo.Subst.html#zipTvSubst"><span class="hs-identifier hs-var">zipTvSubst</span></a></span><span> </span><span class="annot"><span class="annottext">[TcTyVar]
</span><a href="#local-6989586621681174504"><span class="hs-identifier hs-var">cls_tvs</span></a></span><span> </span><span class="annot"><span class="annottext">ThetaType
</span><a href="#local-6989586621681174530"><span class="hs-identifier hs-var">inst_tys</span></a></span><span>
</span><span id="line-122"></span><span>
</span><span id="line-123"></span><span>       </span><span class="hs-special">;</span><span> </span><span class="hs-special">(</span><span id="local-6989586621681174477"><span class="annot"><span class="annottext">[ThetaOrigin]
</span><a href="#local-6989586621681174477"><span class="hs-identifier hs-var">inferred_constraints</span></a></span></span><span class="hs-special">,</span><span> </span><span id="local-6989586621681174476"><span class="annot"><span class="annottext">[TcTyVar]
</span><a href="#local-6989586621681174476"><span class="hs-identifier hs-var">tvs'</span></a></span></span><span class="hs-special">,</span><span> </span><span id="local-6989586621681174475"><span class="annot"><span class="annottext">ThetaType
</span><a href="#local-6989586621681174475"><span class="hs-identifier hs-var">inst_tys'</span></a></span></span><span class="hs-special">)</span><span> </span><span class="hs-glyph">&lt;-</span><span> </span><span class="annot"><span class="annottext">DerivM ([ThetaOrigin], [TcTyVar], ThetaType)
</span><a href="#local-6989586621681174527"><span class="hs-identifier hs-var">infer_constraints</span></a></span><span>
</span><span id="line-124"></span><span>       </span><span class="hs-special">;</span><span> </span><span class="annot"><span class="annottext">IOEnv (Env TcGblEnv TcLclEnv) ()
-&gt; ReaderT DerivEnv (IOEnv (Env TcGblEnv TcLclEnv)) ()
forall (t :: (* -&gt; *) -&gt; * -&gt; *) (m :: * -&gt; *) a.
(MonadTrans t, Monad m) =&gt;
m a -&gt; t m a
</span><a href="../../transformers/src/Control.Monad.Trans.Class.html#lift"><span class="hs-identifier hs-var">lift</span></a></span><span> </span><span class="annot"><span class="annottext">(IOEnv (Env TcGblEnv TcLclEnv) ()
 -&gt; ReaderT DerivEnv (IOEnv (Env TcGblEnv TcLclEnv)) ())
-&gt; IOEnv (Env TcGblEnv TcLclEnv) ()
-&gt; ReaderT DerivEnv (IOEnv (Env TcGblEnv TcLclEnv)) ()
forall a b. (a -&gt; b) -&gt; a -&gt; b
</span><a href="../../base/src/GHC.Base.html#%24"><span class="hs-operator hs-var">$</span></a></span><span> </span><span class="annot"><span class="annottext">String -&gt; SDoc -&gt; IOEnv (Env TcGblEnv TcLclEnv) ()
</span><a href="GHC.Tc.Utils.Monad.html#traceTc"><span class="hs-identifier hs-var">traceTc</span></a></span><span> </span><span class="annot"><span class="annottext">String
</span><span class="hs-string">&quot;inferConstraints&quot;</span></span><span> </span><span class="annot"><span class="annottext">(SDoc -&gt; IOEnv (Env TcGblEnv TcLclEnv) ())
-&gt; SDoc -&gt; IOEnv (Env TcGblEnv TcLclEnv) ()
forall a b. (a -&gt; b) -&gt; a -&gt; b
</span><a href="../../base/src/GHC.Base.html#%24"><span class="hs-operator hs-var">$</span></a></span><span> </span><span class="annot"><span class="annottext">[SDoc] -&gt; SDoc
</span><a href="GHC.Utils.Outputable.html#vcat"><span class="hs-identifier hs-var">vcat</span></a></span><span>
</span><span id="line-125"></span><span>              </span><span class="hs-special">[</span><span> </span><span class="annot"><span class="annottext">Class -&gt; SDoc
forall a. Outputable a =&gt; a -&gt; SDoc
</span><a href="GHC.Utils.Outputable.html#ppr"><span class="hs-identifier hs-var">ppr</span></a></span><span> </span><span class="annot"><span class="annottext">Class
</span><a href="#local-6989586621681174532"><span class="hs-identifier hs-var">main_cls</span></a></span><span> </span><span class="annot"><span class="annottext">SDoc -&gt; SDoc -&gt; SDoc
</span><a href="GHC.Utils.Outputable.html#%3C%2B%3E"><span class="hs-operator hs-var">&lt;+&gt;</span></a></span><span> </span><span class="annot"><span class="annottext">ThetaType -&gt; SDoc
forall a. Outputable a =&gt; a -&gt; SDoc
</span><a href="GHC.Utils.Outputable.html#ppr"><span class="hs-identifier hs-var">ppr</span></a></span><span> </span><span class="annot"><span class="annottext">ThetaType
</span><a href="#local-6989586621681174475"><span class="hs-identifier hs-var">inst_tys'</span></a></span><span>
</span><span id="line-126"></span><span>              </span><span class="hs-special">,</span><span> </span><span class="annot"><span class="annottext">[ThetaOrigin] -&gt; SDoc
forall a. Outputable a =&gt; a -&gt; SDoc
</span><a href="GHC.Utils.Outputable.html#ppr"><span class="hs-identifier hs-var">ppr</span></a></span><span> </span><span class="annot"><span class="annottext">[ThetaOrigin]
</span><a href="#local-6989586621681174477"><span class="hs-identifier hs-var">inferred_constraints</span></a></span><span>
</span><span id="line-127"></span><span>              </span><span class="hs-special">]</span><span>
</span><span id="line-128"></span><span>       </span><span class="hs-special">;</span><span> </span><span class="annot"><span class="annottext">([ThetaOrigin], [TcTyVar], ThetaType)
-&gt; DerivM ([ThetaOrigin], [TcTyVar], ThetaType)
forall (m :: * -&gt; *) a. Monad m =&gt; a -&gt; m a
</span><a href="../../base/src/GHC.Base.html#return"><span class="hs-identifier hs-var">return</span></a></span><span> </span><span class="hs-special">(</span><span> </span><span class="annot"><span class="annottext">[ThetaOrigin]
</span><a href="#local-6989586621681174495"><span class="hs-identifier hs-var">sc_constraints</span></a></span><span> </span><span class="annot"><span class="annottext">[ThetaOrigin] -&gt; [ThetaOrigin] -&gt; [ThetaOrigin]
forall a. [a] -&gt; [a] -&gt; [a]
</span><a href="../../base/src/GHC.Base.html#%2B%2B"><span class="hs-operator hs-var">++</span></a></span><span> </span><span class="annot"><span class="annottext">[ThetaOrigin]
</span><a href="#local-6989586621681174477"><span class="hs-identifier hs-var">inferred_constraints</span></a></span><span>
</span><span id="line-129"></span><span>                </span><span class="hs-special">,</span><span> </span><span class="annot"><span class="annottext">[TcTyVar]
</span><a href="#local-6989586621681174476"><span class="hs-identifier hs-var">tvs'</span></a></span><span class="hs-special">,</span><span> </span><span class="annot"><span class="annottext">ThetaType
</span><a href="#local-6989586621681174475"><span class="hs-identifier hs-var">inst_tys'</span></a></span><span> </span><span class="hs-special">)</span><span> </span><span class="hs-special">}</span><span>
</span><span id="line-130"></span><span>
</span><span id="line-131"></span><span class="hs-comment">-- | Like 'inferConstraints', but used only in the case of the @stock@ deriving</span><span>
</span><span id="line-132"></span><span class="hs-comment">-- strategy. The constraints are inferred by inspecting the fields of each data</span><span>
</span><span id="line-133"></span><span class="hs-comment">-- constructor. In this example:</span><span>
</span><span id="line-134"></span><span class="hs-comment">--</span><span>
</span><span id="line-135"></span><span class="hs-comment">-- &gt; data Foo = MkFoo Int Char deriving Show</span><span>
</span><span id="line-136"></span><span class="hs-comment">--</span><span>
</span><span id="line-137"></span><span class="hs-comment">-- We would infer the following constraints ('ThetaOrigin's):</span><span>
</span><span id="line-138"></span><span class="hs-comment">--</span><span>
</span><span id="line-139"></span><span class="hs-comment">-- &gt; (Show Int, Show Char)</span><span>
</span><span id="line-140"></span><span class="hs-comment">--</span><span>
</span><span id="line-141"></span><span class="hs-comment">-- Note that this function also returns the type variables ('TyVar's) and</span><span>
</span><span id="line-142"></span><span class="hs-comment">-- class arguments ('TcType's) for the resulting instance. This is because</span><span>
</span><span id="line-143"></span><span class="hs-comment">-- when deriving 'Functor'-like classes, we must sometimes perform kind</span><span>
</span><span id="line-144"></span><span class="hs-comment">-- substitutions to ensure the resulting instance is well kinded, which may</span><span>
</span><span id="line-145"></span><span class="hs-comment">-- affect the type variables and class arguments. In this example:</span><span>
</span><span id="line-146"></span><span class="hs-comment">--</span><span>
</span><span id="line-147"></span><span class="hs-comment">-- &gt; newtype Compose (f :: k -&gt; Type) (g :: Type -&gt; k) (a :: Type) =</span><span>
</span><span id="line-148"></span><span class="hs-comment">-- &gt;   Compose (f (g a)) deriving stock Functor</span><span>
</span><span id="line-149"></span><span class="hs-comment">--</span><span>
</span><span id="line-150"></span><span class="hs-comment">-- We must unify @k@ with @Type@ in order for the resulting 'Functor' instance</span><span>
</span><span id="line-151"></span><span class="hs-comment">-- to be well kinded, so we return @[]@/@[Type, f, g]@ for the</span><span>
</span><span id="line-152"></span><span class="hs-comment">-- 'TyVar's/'TcType's, /not/ @[k]@/@[k, f, g]@.</span><span>
</span><span id="line-153"></span><span class="hs-comment">-- See Note [Inferring the instance context].</span><span>
</span><span id="line-154"></span><span class="annot"><a href="GHC.Tc.Deriv.Infer.html#inferConstraintsStock"><span class="hs-identifier hs-type">inferConstraintsStock</span></a></span><span> </span><span class="hs-glyph">::</span><span> </span><span class="annot"><a href="GHC.Tc.Deriv.Utils.html#DerivInstTys"><span class="hs-identifier hs-type">DerivInstTys</span></a></span><span>
</span><span id="line-155"></span><span>                      </span><span class="hs-glyph">-&gt;</span><span> </span><span class="annot"><a href="GHC.Tc.Deriv.Utils.html#DerivM"><span class="hs-identifier hs-type">DerivM</span></a></span><span> </span><span class="hs-special">(</span><span class="hs-special">[</span><span class="annot"><a href="GHC.Tc.Deriv.Utils.html#ThetaOrigin"><span class="hs-identifier hs-type">ThetaOrigin</span></a></span><span class="hs-special">]</span><span class="hs-special">,</span><span> </span><span class="hs-special">[</span><span class="annot"><a href="GHC.Types.Var.html#TyVar"><span class="hs-identifier hs-type">TyVar</span></a></span><span class="hs-special">]</span><span class="hs-special">,</span><span> </span><span class="hs-special">[</span><span class="annot"><a href="GHC.Tc.Utils.TcType.html#TcType"><span class="hs-identifier hs-type">TcType</span></a></span><span class="hs-special">]</span><span class="hs-special">)</span><span>
</span><span id="line-156"></span><span id="inferConstraintsStock"><span class="annot"><span class="annottext">inferConstraintsStock :: DerivInstTys -&gt; DerivM ([ThetaOrigin], [TcTyVar], ThetaType)
</span><a href="GHC.Tc.Deriv.Infer.html#inferConstraintsStock"><span class="hs-identifier hs-var hs-var">inferConstraintsStock</span></a></span></span><span> </span><span class="hs-special">(</span><span class="annot"><a href="GHC.Tc.Deriv.Utils.html#DerivInstTys"><span class="hs-identifier hs-type">DerivInstTys</span></a></span><span> </span><span class="hs-special">{</span><span> </span><span class="annot"><span class="annottext">dit_cls_tys :: DerivInstTys -&gt; ThetaType
</span><a href="GHC.Tc.Deriv.Utils.html#dit_cls_tys"><span class="hs-identifier hs-var">dit_cls_tys</span></a></span><span>     </span><span class="hs-glyph">=</span><span> </span><span id="local-6989586621681174472"><span class="annot"><span class="annottext">ThetaType
</span><a href="#local-6989586621681174472"><span class="hs-identifier hs-var">cls_tys</span></a></span></span><span>
</span><span id="line-157"></span><span>                                    </span><span class="hs-special">,</span><span> </span><span class="annot"><span class="annottext">dit_tc :: DerivInstTys -&gt; TyCon
</span><a href="GHC.Tc.Deriv.Utils.html#dit_tc"><span class="hs-identifier hs-var">dit_tc</span></a></span><span>          </span><span class="hs-glyph">=</span><span> </span><span id="local-6989586621681174470"><span class="annot"><span class="annottext">TyCon
</span><a href="#local-6989586621681174470"><span class="hs-identifier hs-var">tc</span></a></span></span><span>
</span><span id="line-158"></span><span>                                    </span><span class="hs-special">,</span><span> </span><span class="annot"><span class="annottext">dit_tc_args :: DerivInstTys -&gt; ThetaType
</span><a href="GHC.Tc.Deriv.Utils.html#dit_tc_args"><span class="hs-identifier hs-var">dit_tc_args</span></a></span><span>     </span><span class="hs-glyph">=</span><span> </span><span id="local-6989586621681174468"><span class="annot"><span class="annottext">ThetaType
</span><a href="#local-6989586621681174468"><span class="hs-identifier hs-var">tc_args</span></a></span></span><span>
</span><span id="line-159"></span><span>                                    </span><span class="hs-special">,</span><span> </span><span class="annot"><span class="annottext">dit_rep_tc :: DerivInstTys -&gt; TyCon
</span><a href="GHC.Tc.Deriv.Utils.html#dit_rep_tc"><span class="hs-identifier hs-var">dit_rep_tc</span></a></span><span>      </span><span class="hs-glyph">=</span><span> </span><span id="local-6989586621681174466"><span class="annot"><span class="annottext">TyCon
</span><a href="#local-6989586621681174466"><span class="hs-identifier hs-var">rep_tc</span></a></span></span><span>
</span><span id="line-160"></span><span>                                    </span><span class="hs-special">,</span><span> </span><span class="annot"><span class="annottext">dit_rep_tc_args :: DerivInstTys -&gt; ThetaType
</span><a href="GHC.Tc.Deriv.Utils.html#dit_rep_tc_args"><span class="hs-identifier hs-var">dit_rep_tc_args</span></a></span><span> </span><span class="hs-glyph">=</span><span> </span><span id="local-6989586621681174464"><span class="annot"><span class="annottext">ThetaType
</span><a href="#local-6989586621681174464"><span class="hs-identifier hs-var">rep_tc_args</span></a></span></span><span> </span><span class="hs-special">}</span><span class="hs-special">)</span><span>
</span><span id="line-161"></span><span>  </span><span class="hs-glyph">=</span><span> </span><span class="hs-keyword">do</span><span> </span><span class="annot"><a href="GHC.Tc.Deriv.Utils.html#DerivEnv"><span class="hs-identifier hs-type">DerivEnv</span></a></span><span> </span><span class="hs-special">{</span><span> </span><span class="annot"><span class="annottext">denv_tvs :: DerivEnv -&gt; [TcTyVar]
</span><a href="GHC.Tc.Deriv.Utils.html#denv_tvs"><span class="hs-identifier hs-var">denv_tvs</span></a></span><span>      </span><span class="hs-glyph">=</span><span> </span><span id="local-6989586621681174463"><span class="annot"><span class="annottext">[TcTyVar]
</span><a href="#local-6989586621681174463"><span class="hs-identifier hs-var">tvs</span></a></span></span><span>
</span><span id="line-162"></span><span>                </span><span class="hs-special">,</span><span> </span><span class="annot"><span class="annottext">denv_cls :: DerivEnv -&gt; Class
</span><a href="GHC.Tc.Deriv.Utils.html#denv_cls"><span class="hs-identifier hs-var">denv_cls</span></a></span><span>      </span><span class="hs-glyph">=</span><span> </span><span id="local-6989586621681174462"><span class="annot"><span class="annottext">Class
</span><a href="#local-6989586621681174462"><span class="hs-identifier hs-var">main_cls</span></a></span></span><span>
</span><span id="line-163"></span><span>                </span><span class="hs-special">,</span><span> </span><span class="annot"><span class="annottext">denv_inst_tys :: DerivEnv -&gt; ThetaType
</span><a href="GHC.Tc.Deriv.Utils.html#denv_inst_tys"><span class="hs-identifier hs-var">denv_inst_tys</span></a></span><span> </span><span class="hs-glyph">=</span><span> </span><span id="local-6989586621681174461"><span class="annot"><span class="annottext">ThetaType
</span><a href="#local-6989586621681174461"><span class="hs-identifier hs-var">inst_tys</span></a></span></span><span> </span><span class="hs-special">}</span><span> </span><span class="hs-glyph">&lt;-</span><span> </span><span class="annot"><span class="annottext">ReaderT DerivEnv (IOEnv (Env TcGblEnv TcLclEnv)) DerivEnv
forall (m :: * -&gt; *) r. Monad m =&gt; ReaderT r m r
</span><a href="../../transformers/src/Control.Monad.Trans.Reader.html#ask"><span class="hs-identifier hs-var">ask</span></a></span><span>
</span><span id="line-164"></span><span>       </span><span id="local-6989586621681174460"><span class="annot"><span class="annottext">Bool
</span><a href="#local-6989586621681174460"><span class="hs-identifier hs-var">wildcard</span></a></span></span><span> </span><span class="hs-glyph">&lt;-</span><span> </span><span class="annot"><span class="annottext">DerivM Bool
</span><a href="GHC.Tc.Deriv.Utils.html#isStandaloneWildcardDeriv"><span class="hs-identifier hs-var">isStandaloneWildcardDeriv</span></a></span><span>
</span><span id="line-165"></span><span>
</span><span id="line-166"></span><span>       </span><span class="hs-keyword">let</span><span> </span><span id="local-6989586621681174459"><span class="annot"><span class="annottext">inst_ty :: PredType
</span><a href="#local-6989586621681174459"><span class="hs-identifier hs-var hs-var">inst_ty</span></a></span></span><span>    </span><span class="hs-glyph">=</span><span> </span><span class="annot"><span class="annottext">TyCon -&gt; ThetaType -&gt; PredType
</span><a href="GHC.Core.TyCo.Rep.html#mkTyConApp"><span class="hs-identifier hs-var">mkTyConApp</span></a></span><span> </span><span class="annot"><span class="annottext">TyCon
</span><a href="#local-6989586621681174470"><span class="hs-identifier hs-var">tc</span></a></span><span> </span><span class="annot"><span class="annottext">ThetaType
</span><a href="#local-6989586621681174468"><span class="hs-identifier hs-var">tc_args</span></a></span><span>
</span><span id="line-167"></span><span>           </span><span id="local-6989586621681174457"><span class="annot"><span class="annottext">tc_binders :: [TyConBinder]
</span><a href="#local-6989586621681174457"><span class="hs-identifier hs-var hs-var">tc_binders</span></a></span></span><span> </span><span class="hs-glyph">=</span><span> </span><span class="annot"><span class="annottext">TyCon -&gt; [TyConBinder]
</span><a href="GHC.Core.TyCon.html#tyConBinders"><span class="hs-identifier hs-var hs-var">tyConBinders</span></a></span><span> </span><span class="annot"><span class="annottext">TyCon
</span><a href="#local-6989586621681174466"><span class="hs-identifier hs-var">rep_tc</span></a></span><span>
</span><span id="line-168"></span><span>           </span><span id="local-6989586621681174455"><span class="annot"><span class="annottext">choose_level :: TyConBinder -&gt; TypeOrKind
</span><a href="#local-6989586621681174455"><span class="hs-identifier hs-var hs-var">choose_level</span></a></span></span><span> </span><span id="local-6989586621681174454"><span class="annot"><span class="annottext">TyConBinder
</span><a href="#local-6989586621681174454"><span class="hs-identifier hs-var">bndr</span></a></span></span><span>
</span><span id="line-169"></span><span>             </span><span class="hs-glyph">|</span><span> </span><span class="annot"><span class="annottext">TyConBinder -&gt; Bool
</span><a href="GHC.Core.TyCon.html#isNamedTyConBinder"><span class="hs-identifier hs-var">isNamedTyConBinder</span></a></span><span> </span><span class="annot"><span class="annottext">TyConBinder
</span><a href="#local-6989586621681174454"><span class="hs-identifier hs-var">bndr</span></a></span><span> </span><span class="hs-glyph">=</span><span> </span><span class="annot"><span class="annottext">TypeOrKind
</span><a href="GHC.Types.Basic.html#KindLevel"><span class="hs-identifier hs-var">KindLevel</span></a></span><span>
</span><span id="line-170"></span><span>             </span><span class="hs-glyph">|</span><span> </span><span class="annot"><span class="annottext">Bool
</span><a href="../../base/src/GHC.Base.html#otherwise"><span class="hs-identifier hs-var">otherwise</span></a></span><span>               </span><span class="hs-glyph">=</span><span> </span><span class="annot"><span class="annottext">TypeOrKind
</span><a href="GHC.Types.Basic.html#TypeLevel"><span class="hs-identifier hs-var">TypeLevel</span></a></span><span>
</span><span id="line-171"></span><span>           </span><span id="local-6989586621681174451"><span class="annot"><span class="annottext">t_or_ks :: [TypeOrKind]
</span><a href="#local-6989586621681174451"><span class="hs-identifier hs-var hs-var">t_or_ks</span></a></span></span><span> </span><span class="hs-glyph">=</span><span> </span><span class="annot"><span class="annottext">(TyConBinder -&gt; TypeOrKind) -&gt; [TyConBinder] -&gt; [TypeOrKind]
forall a b. (a -&gt; b) -&gt; [a] -&gt; [b]
</span><a href="../../base/src/GHC.Base.html#map"><span class="hs-identifier hs-var">map</span></a></span><span> </span><span class="annot"><span class="annottext">TyConBinder -&gt; TypeOrKind
</span><a href="#local-6989586621681174455"><span class="hs-identifier hs-var">choose_level</span></a></span><span> </span><span class="annot"><span class="annottext">[TyConBinder]
</span><a href="#local-6989586621681174457"><span class="hs-identifier hs-var">tc_binders</span></a></span><span> </span><span class="annot"><span class="annottext">[TypeOrKind] -&gt; [TypeOrKind] -&gt; [TypeOrKind]
forall a. [a] -&gt; [a] -&gt; [a]
</span><a href="../../base/src/GHC.Base.html#%2B%2B"><span class="hs-operator hs-var">++</span></a></span><span> </span><span class="annot"><span class="annottext">TypeOrKind -&gt; [TypeOrKind]
forall a. a -&gt; [a]
</span><a href="../../base/src/GHC.List.html#repeat"><span class="hs-identifier hs-var">repeat</span></a></span><span> </span><span class="annot"><span class="annottext">TypeOrKind
</span><a href="GHC.Types.Basic.html#TypeLevel"><span class="hs-identifier hs-var">TypeLevel</span></a></span><span>
</span><span id="line-172"></span><span>              </span><span class="hs-comment">-- want to report *kind* errors when possible</span><span>
</span><span id="line-173"></span><span>
</span><span id="line-174"></span><span>              </span><span class="hs-comment">-- Constraints arising from the arguments of each constructor</span><span>
</span><span id="line-175"></span><span>           </span><span class="annot"><a href="#local-6989586621681174449"><span class="hs-identifier hs-type">con_arg_constraints</span></a></span><span>
</span><span id="line-176"></span><span>             </span><span class="hs-glyph">::</span><span> </span><span class="hs-special">(</span><span class="annot"><a href="GHC.Tc.Types.Origin.html#CtOrigin"><span class="hs-identifier hs-type">CtOrigin</span></a></span><span> </span><span class="hs-glyph">-&gt;</span><span> </span><span class="annot"><a href="GHC.Types.Basic.html#TypeOrKind"><span class="hs-identifier hs-type">TypeOrKind</span></a></span><span>
</span><span id="line-177"></span><span>                          </span><span class="hs-glyph">-&gt;</span><span> </span><span class="annot"><a href="GHC.Core.TyCo.Rep.html#Type"><span class="hs-identifier hs-type">Type</span></a></span><span>
</span><span id="line-178"></span><span>                          </span><span class="hs-glyph">-&gt;</span><span> </span><span class="hs-special">[</span><span class="hs-special">(</span><span class="hs-special">[</span><span class="annot"><a href="GHC.Tc.Deriv.Utils.html#PredOrigin"><span class="hs-identifier hs-type">PredOrigin</span></a></span><span class="hs-special">]</span><span class="hs-special">,</span><span> </span><span class="annot"><a href="../../base/src/GHC.Maybe.html#Maybe"><span class="hs-identifier hs-type">Maybe</span></a></span><span> </span><span class="annot"><a href="GHC.Core.TyCo.Subst.html#TCvSubst"><span class="hs-identifier hs-type">TCvSubst</span></a></span><span class="hs-special">)</span><span class="hs-special">]</span><span class="hs-special">)</span><span>
</span><span id="line-179"></span><span>             </span><span class="hs-glyph">-&gt;</span><span> </span><span class="hs-special">(</span><span class="hs-special">[</span><span class="annot"><a href="GHC.Tc.Deriv.Utils.html#ThetaOrigin"><span class="hs-identifier hs-type">ThetaOrigin</span></a></span><span class="hs-special">]</span><span class="hs-special">,</span><span> </span><span class="hs-special">[</span><span class="annot"><a href="GHC.Types.Var.html#TyVar"><span class="hs-identifier hs-type">TyVar</span></a></span><span class="hs-special">]</span><span class="hs-special">,</span><span> </span><span class="hs-special">[</span><span class="annot"><a href="GHC.Tc.Utils.TcType.html#TcType"><span class="hs-identifier hs-type">TcType</span></a></span><span class="hs-special">]</span><span class="hs-special">)</span><span>
</span><span id="line-180"></span><span>           </span><span id="local-6989586621681174449"><span class="annot"><span class="annottext">con_arg_constraints :: (CtOrigin
 -&gt; TypeOrKind -&gt; PredType -&gt; [([PredOrigin], Maybe TCvSubst)])
-&gt; ([ThetaOrigin], [TcTyVar], ThetaType)
</span><a href="#local-6989586621681174449"><span class="hs-identifier hs-var hs-var">con_arg_constraints</span></a></span></span><span> </span><span id="local-6989586621681174447"><span class="annot"><span class="annottext">CtOrigin
-&gt; TypeOrKind -&gt; PredType -&gt; [([PredOrigin], Maybe TCvSubst)]
</span><a href="#local-6989586621681174447"><span class="hs-identifier hs-var">get_arg_constraints</span></a></span></span><span>
</span><span id="line-181"></span><span>             </span><span class="hs-glyph">=</span><span> </span><span class="hs-keyword">let</span><span> </span><span class="hs-special">(</span><span id="local-6989586621681174442"><span class="annot"><span class="annottext">[[PredOrigin]]
</span><a href="#local-6989586621681174442"><span class="hs-identifier hs-var">predss</span></a></span></span><span class="hs-special">,</span><span> </span><span id="local-6989586621681174441"><span class="annot"><span class="annottext">[Maybe TCvSubst]
</span><a href="#local-6989586621681174441"><span class="hs-identifier hs-var">mbSubsts</span></a></span></span><span class="hs-special">)</span><span> </span><span class="hs-glyph">=</span><span> </span><span class="annot"><span class="annottext">[([PredOrigin], Maybe TCvSubst)]
-&gt; ([[PredOrigin]], [Maybe TCvSubst])
forall a b. [(a, b)] -&gt; ([a], [b])
</span><a href="../../base/src/GHC.List.html#unzip"><span class="hs-identifier hs-var">unzip</span></a></span><span>
</span><span id="line-182"></span><span>                     </span><span class="hs-special">[</span><span> </span><span class="annot"><span class="annottext">([PredOrigin], Maybe TCvSubst)
</span><a href="#local-6989586621681174439"><span class="hs-identifier hs-var">preds_and_mbSubst</span></a></span><span>
</span><span id="line-183"></span><span>                     </span><span class="hs-glyph">|</span><span> </span><span id="local-6989586621681174438"><span class="annot"><span class="annottext">DataCon
</span><a href="#local-6989586621681174438"><span class="hs-identifier hs-var">data_con</span></a></span></span><span> </span><span class="hs-glyph">&lt;-</span><span> </span><span class="annot"><span class="annottext">TyCon -&gt; [DataCon]
</span><a href="GHC.Core.TyCon.html#tyConDataCons"><span class="hs-identifier hs-var">tyConDataCons</span></a></span><span> </span><span class="annot"><span class="annottext">TyCon
</span><a href="#local-6989586621681174466"><span class="hs-identifier hs-var">rep_tc</span></a></span><span>
</span><span id="line-184"></span><span>                     </span><span class="hs-special">,</span><span> </span><span class="hs-special">(</span><span id="local-6989586621681174436"><span class="annot"><span class="annottext">Int
</span><a href="#local-6989586621681174436"><span class="hs-identifier hs-var">arg_n</span></a></span></span><span class="hs-special">,</span><span> </span><span id="local-6989586621681174435"><span class="annot"><span class="annottext">TypeOrKind
</span><a href="#local-6989586621681174435"><span class="hs-identifier hs-var">arg_t_or_k</span></a></span></span><span class="hs-special">,</span><span> </span><span id="local-6989586621681174434"><span class="annot"><span class="annottext">Scaled PredType
</span><a href="#local-6989586621681174434"><span class="hs-identifier hs-var">arg_ty</span></a></span></span><span class="hs-special">)</span><span>
</span><span id="line-185"></span><span>                         </span><span class="hs-glyph">&lt;-</span><span> </span><span class="annot"><span class="annottext">[Int]
-&gt; [TypeOrKind]
-&gt; [Scaled PredType]
-&gt; [(Int, TypeOrKind, Scaled PredType)]
forall a b c. [a] -&gt; [b] -&gt; [c] -&gt; [(a, b, c)]
</span><a href="../../base/src/GHC.List.html#zip3"><span class="hs-identifier hs-var">zip3</span></a></span><span> </span><span class="hs-special">[</span><span class="annot"><span class="annottext">Int
</span><span class="hs-number">1</span></span><span class="hs-glyph">..</span><span class="hs-special">]</span><span> </span><span class="annot"><span class="annottext">[TypeOrKind]
</span><a href="#local-6989586621681174451"><span class="hs-identifier hs-var">t_or_ks</span></a></span><span> </span><span class="annot"><span class="annottext">([Scaled PredType] -&gt; [(Int, TypeOrKind, Scaled PredType)])
-&gt; [Scaled PredType] -&gt; [(Int, TypeOrKind, Scaled PredType)]
forall a b. (a -&gt; b) -&gt; a -&gt; b
</span><a href="../../base/src/GHC.Base.html#%24"><span class="hs-operator hs-var">$</span></a></span><span>
</span><span id="line-186"></span><span>                            </span><span class="annot"><span class="annottext">DataCon -&gt; ThetaType -&gt; [Scaled PredType]
</span><a href="GHC.Core.DataCon.html#dataConInstOrigArgTys"><span class="hs-identifier hs-var">dataConInstOrigArgTys</span></a></span><span> </span><span class="annot"><span class="annottext">DataCon
</span><a href="#local-6989586621681174438"><span class="hs-identifier hs-var">data_con</span></a></span><span> </span><span class="annot"><span class="annottext">ThetaType
</span><a href="#local-6989586621681174431"><span class="hs-identifier hs-var">all_rep_tc_args</span></a></span><span>
</span><span id="line-187"></span><span>                       </span><span class="hs-comment">-- No constraints for unlifted types</span><span>
</span><span id="line-188"></span><span>                       </span><span class="hs-comment">-- See Note [Deriving and unboxed types]</span><span>
</span><span id="line-189"></span><span>                     </span><span class="hs-special">,</span><span> </span><span class="annot"><span class="annottext">Bool -&gt; Bool
</span><span class="hs-identifier hs-var">not</span></span><span> </span><span class="hs-special">(</span><span class="annot"><span class="annottext">HasDebugCallStack =&gt; PredType -&gt; Bool
PredType -&gt; Bool
</span><a href="GHC.Core.Type.html#isUnliftedType"><span class="hs-identifier hs-var">isUnliftedType</span></a></span><span> </span><span class="hs-special">(</span><span class="annot"><span class="annottext">Scaled PredType -&gt; PredType
forall a. Scaled a -&gt; a
</span><a href="GHC.Core.Type.html#irrelevantMult"><span class="hs-identifier hs-var">irrelevantMult</span></a></span><span> </span><span class="annot"><span class="annottext">Scaled PredType
</span><a href="#local-6989586621681174434"><span class="hs-identifier hs-var">arg_ty</span></a></span><span class="hs-special">)</span><span class="hs-special">)</span><span>
</span><span id="line-190"></span><span>                     </span><span class="hs-special">,</span><span> </span><span class="hs-keyword">let</span><span> </span><span id="local-6989586621681174428"><span class="annot"><span class="annottext">orig :: CtOrigin
</span><a href="#local-6989586621681174428"><span class="hs-identifier hs-var hs-var">orig</span></a></span></span><span> </span><span class="hs-glyph">=</span><span> </span><span class="annot"><span class="annottext">DataCon -&gt; Int -&gt; Bool -&gt; CtOrigin
</span><a href="GHC.Tc.Types.Origin.html#DerivOriginDC"><span class="hs-identifier hs-var">DerivOriginDC</span></a></span><span> </span><span class="annot"><span class="annottext">DataCon
</span><a href="#local-6989586621681174438"><span class="hs-identifier hs-var">data_con</span></a></span><span> </span><span class="annot"><span class="annottext">Int
</span><a href="#local-6989586621681174436"><span class="hs-identifier hs-var">arg_n</span></a></span><span> </span><span class="annot"><span class="annottext">Bool
</span><a href="#local-6989586621681174460"><span class="hs-identifier hs-var">wildcard</span></a></span><span>
</span><span id="line-191"></span><span>                     </span><span class="hs-special">,</span><span> </span><span id="local-6989586621681174439"><span class="annot"><span class="annottext">([PredOrigin], Maybe TCvSubst)
</span><a href="#local-6989586621681174439"><span class="hs-identifier hs-var">preds_and_mbSubst</span></a></span></span><span>
</span><span id="line-192"></span><span>                         </span><span class="hs-glyph">&lt;-</span><span> </span><span class="annot"><span class="annottext">CtOrigin
-&gt; TypeOrKind -&gt; PredType -&gt; [([PredOrigin], Maybe TCvSubst)]
</span><a href="#local-6989586621681174447"><span class="hs-identifier hs-var">get_arg_constraints</span></a></span><span> </span><span class="annot"><span class="annottext">CtOrigin
</span><a href="#local-6989586621681174428"><span class="hs-identifier hs-var">orig</span></a></span><span> </span><span class="annot"><span class="annottext">TypeOrKind
</span><a href="#local-6989586621681174435"><span class="hs-identifier hs-var">arg_t_or_k</span></a></span><span> </span><span class="hs-special">(</span><span class="annot"><span class="annottext">Scaled PredType -&gt; PredType
forall a. Scaled a -&gt; a
</span><a href="GHC.Core.Type.html#irrelevantMult"><span class="hs-identifier hs-var">irrelevantMult</span></a></span><span> </span><span class="annot"><span class="annottext">Scaled PredType
</span><a href="#local-6989586621681174434"><span class="hs-identifier hs-var">arg_ty</span></a></span><span class="hs-special">)</span><span>
</span><span id="line-193"></span><span>                     </span><span class="hs-special">]</span><span>
</span><span id="line-194"></span><span>                   </span><span id="local-6989586621681174425"><span class="annot"><span class="annottext">preds :: [PredOrigin]
</span><a href="#local-6989586621681174425"><span class="hs-identifier hs-var hs-var">preds</span></a></span></span><span> </span><span class="hs-glyph">=</span><span> </span><span class="annot"><span class="annottext">[[PredOrigin]] -&gt; [PredOrigin]
forall (t :: * -&gt; *) a. Foldable t =&gt; t [a] -&gt; [a]
</span><a href="../../base/src/Data.Foldable.html#concat"><span class="hs-identifier hs-var">concat</span></a></span><span> </span><span class="annot"><span class="annottext">[[PredOrigin]]
</span><a href="#local-6989586621681174442"><span class="hs-identifier hs-var">predss</span></a></span><span>
</span><span id="line-195"></span><span>                   </span><span class="hs-comment">-- If the constraints require a subtype to be of kind</span><span>
</span><span id="line-196"></span><span>                   </span><span class="hs-comment">-- (* -&gt; *) (which is the case for functor-like</span><span>
</span><span id="line-197"></span><span>                   </span><span class="hs-comment">-- constraints), then we explicitly unify the subtype's</span><span>
</span><span id="line-198"></span><span>                   </span><span class="hs-comment">-- kinds with (* -&gt; *).</span><span>
</span><span id="line-199"></span><span>                   </span><span class="hs-comment">-- See Note [Inferring the instance context]</span><span>
</span><span id="line-200"></span><span>                   </span><span id="local-6989586621681174422"><span class="annot"><span class="annottext">subst :: TCvSubst
</span><a href="#local-6989586621681174422"><span class="hs-identifier hs-var hs-var">subst</span></a></span></span><span>        </span><span class="hs-glyph">=</span><span> </span><span class="annot"><span class="annottext">(TCvSubst -&gt; TCvSubst -&gt; TCvSubst)
-&gt; TCvSubst -&gt; [TCvSubst] -&gt; TCvSubst
forall (t :: * -&gt; *) b a.
Foldable t =&gt;
(b -&gt; a -&gt; b) -&gt; b -&gt; t a -&gt; b
</span><a href="../../base/src/Data.Foldable.html#foldl%27"><span class="hs-identifier hs-var">foldl'</span></a></span><span> </span><span class="annot"><span class="annottext">TCvSubst -&gt; TCvSubst -&gt; TCvSubst
</span><a href="GHC.Core.TyCo.Subst.html#composeTCvSubst"><span class="hs-identifier hs-var">composeTCvSubst</span></a></span><span>
</span><span id="line-201"></span><span>                                         </span><span class="annot"><span class="annottext">TCvSubst
</span><a href="GHC.Core.TyCo.Subst.html#emptyTCvSubst"><span class="hs-identifier hs-var">emptyTCvSubst</span></a></span><span> </span><span class="hs-special">(</span><span class="annot"><span class="annottext">[Maybe TCvSubst] -&gt; [TCvSubst]
forall a. [Maybe a] -&gt; [a]
</span><a href="../../base/src/Data.Maybe.html#catMaybes"><span class="hs-identifier hs-var">catMaybes</span></a></span><span> </span><span class="annot"><span class="annottext">[Maybe TCvSubst]
</span><a href="#local-6989586621681174441"><span class="hs-identifier hs-var">mbSubsts</span></a></span><span class="hs-special">)</span><span>
</span><span id="line-202"></span><span>                   </span><span id="local-6989586621681174417"><span class="annot"><span class="annottext">unmapped_tvs :: [TcTyVar]
</span><a href="#local-6989586621681174417"><span class="hs-identifier hs-var hs-var">unmapped_tvs</span></a></span></span><span> </span><span class="hs-glyph">=</span><span> </span><span class="annot"><span class="annottext">(TcTyVar -&gt; Bool) -&gt; [TcTyVar] -&gt; [TcTyVar]
forall a. (a -&gt; Bool) -&gt; [a] -&gt; [a]
</span><a href="../../base/src/GHC.List.html#filter"><span class="hs-identifier hs-var">filter</span></a></span><span> </span><span class="hs-special">(</span><span class="hs-glyph">\</span><span id="local-6989586621681174416"><span class="annot"><span class="annottext">TcTyVar
</span><a href="#local-6989586621681174416"><span class="hs-identifier hs-var">v</span></a></span></span><span> </span><span class="hs-glyph">-&gt;</span><span> </span><span class="annot"><span class="annottext">TcTyVar
</span><a href="#local-6989586621681174416"><span class="hs-identifier hs-var">v</span></a></span><span> </span><span class="annot"><span class="annottext">TcTyVar -&gt; TCvSubst -&gt; Bool
</span><a href="GHC.Core.TyCo.Subst.html#notElemTCvSubst"><span class="hs-operator hs-var">`notElemTCvSubst`</span></a></span><span> </span><span class="annot"><span class="annottext">TCvSubst
</span><a href="#local-6989586621681174422"><span class="hs-identifier hs-var">subst</span></a></span><span>
</span><span id="line-203"></span><span>                                             </span><span class="annot"><span class="annottext">Bool -&gt; Bool -&gt; Bool
</span><span class="hs-operator hs-var">&amp;&amp;</span></span><span> </span><span class="annot"><span class="annottext">Bool -&gt; Bool
</span><span class="hs-identifier hs-var">not</span></span><span> </span><span class="hs-special">(</span><span class="annot"><span class="annottext">TcTyVar
</span><a href="#local-6989586621681174416"><span class="hs-identifier hs-var">v</span></a></span><span> </span><span class="annot"><span class="annottext">TcTyVar -&gt; TCvSubst -&gt; Bool
</span><a href="GHC.Core.TyCo.Subst.html#isInScope"><span class="hs-operator hs-var">`isInScope`</span></a></span><span> </span><span class="annot"><span class="annottext">TCvSubst
</span><a href="#local-6989586621681174422"><span class="hs-identifier hs-var">subst</span></a></span><span class="hs-special">)</span><span class="hs-special">)</span><span> </span><span class="annot"><span class="annottext">[TcTyVar]
</span><a href="#local-6989586621681174463"><span class="hs-identifier hs-var">tvs</span></a></span><span>
</span><span id="line-204"></span><span>                   </span><span class="hs-special">(</span><span id="local-6989586621681174411"><span class="annot"><span class="annottext">TCvSubst
</span><a href="#local-6989586621681174411"><span class="hs-identifier hs-var">subst'</span></a></span></span><span class="hs-special">,</span><span> </span><span class="annot"><span class="annottext">[TcTyVar]
</span><span class="hs-identifier">_</span></span><span class="hs-special">)</span><span>  </span><span class="hs-glyph">=</span><span> </span><span class="annot"><span class="annottext">HasCallStack =&gt; TCvSubst -&gt; [TcTyVar] -&gt; (TCvSubst, [TcTyVar])
TCvSubst -&gt; [TcTyVar] -&gt; (TCvSubst, [TcTyVar])
</span><a href="GHC.Core.TyCo.Subst.html#substTyVarBndrs"><span class="hs-identifier hs-var">substTyVarBndrs</span></a></span><span> </span><span class="annot"><span class="annottext">TCvSubst
</span><a href="#local-6989586621681174422"><span class="hs-identifier hs-var">subst</span></a></span><span> </span><span class="annot"><span class="annottext">[TcTyVar]
</span><a href="#local-6989586621681174417"><span class="hs-identifier hs-var">unmapped_tvs</span></a></span><span>
</span><span id="line-205"></span><span>                   </span><span id="local-6989586621681174407"><span class="annot"><span class="annottext">preds' :: [PredOrigin]
</span><a href="#local-6989586621681174407"><span class="hs-identifier hs-var hs-var">preds'</span></a></span></span><span>       </span><span class="hs-glyph">=</span><span> </span><span class="annot"><span class="annottext">(PredOrigin -&gt; PredOrigin) -&gt; [PredOrigin] -&gt; [PredOrigin]
forall a b. (a -&gt; b) -&gt; [a] -&gt; [b]
</span><a href="../../base/src/GHC.Base.html#map"><span class="hs-identifier hs-var">map</span></a></span><span> </span><span class="hs-special">(</span><span class="annot"><span class="annottext">HasCallStack =&gt; TCvSubst -&gt; PredOrigin -&gt; PredOrigin
TCvSubst -&gt; PredOrigin -&gt; PredOrigin
</span><a href="GHC.Tc.Deriv.Utils.html#substPredOrigin"><span class="hs-identifier hs-var">substPredOrigin</span></a></span><span> </span><span class="annot"><span class="annottext">TCvSubst
</span><a href="#local-6989586621681174411"><span class="hs-identifier hs-var">subst'</span></a></span><span class="hs-special">)</span><span> </span><span class="annot"><span class="annottext">[PredOrigin]
</span><a href="#local-6989586621681174425"><span class="hs-identifier hs-var">preds</span></a></span><span>
</span><span id="line-206"></span><span>                   </span><span id="local-6989586621681174403"><span class="annot"><span class="annottext">inst_tys' :: ThetaType
</span><a href="#local-6989586621681174403"><span class="hs-identifier hs-var hs-var">inst_tys'</span></a></span></span><span>    </span><span class="hs-glyph">=</span><span> </span><span class="annot"><span class="annottext">HasCallStack =&gt; TCvSubst -&gt; ThetaType -&gt; ThetaType
TCvSubst -&gt; ThetaType -&gt; ThetaType
</span><a href="GHC.Core.TyCo.Subst.html#substTys"><span class="hs-identifier hs-var">substTys</span></a></span><span> </span><span class="annot"><span class="annottext">TCvSubst
</span><a href="#local-6989586621681174411"><span class="hs-identifier hs-var">subst'</span></a></span><span> </span><span class="annot"><span class="annottext">ThetaType
</span><a href="#local-6989586621681174461"><span class="hs-identifier hs-var">inst_tys</span></a></span><span>
</span><span id="line-207"></span><span>                   </span><span id="local-6989586621681174401"><span class="annot"><span class="annottext">tvs' :: [TcTyVar]
</span><a href="#local-6989586621681174401"><span class="hs-identifier hs-var hs-var">tvs'</span></a></span></span><span>         </span><span class="hs-glyph">=</span><span> </span><span class="annot"><span class="annottext">ThetaType -&gt; [TcTyVar]
</span><a href="GHC.Core.TyCo.FVs.html#tyCoVarsOfTypesWellScoped"><span class="hs-identifier hs-var">tyCoVarsOfTypesWellScoped</span></a></span><span> </span><span class="annot"><span class="annottext">ThetaType
</span><a href="#local-6989586621681174403"><span class="hs-identifier hs-var">inst_tys'</span></a></span><span>
</span><span id="line-208"></span><span>               </span><span class="hs-keyword">in</span><span> </span><span class="hs-special">(</span><span class="hs-special">[</span><span class="annot"><span class="annottext">[PredOrigin] -&gt; ThetaOrigin
</span><a href="GHC.Tc.Deriv.Utils.html#mkThetaOriginFromPreds"><span class="hs-identifier hs-var">mkThetaOriginFromPreds</span></a></span><span> </span><span class="annot"><span class="annottext">[PredOrigin]
</span><a href="#local-6989586621681174407"><span class="hs-identifier hs-var">preds'</span></a></span><span class="hs-special">]</span><span class="hs-special">,</span><span> </span><span class="annot"><span class="annottext">[TcTyVar]
</span><a href="#local-6989586621681174401"><span class="hs-identifier hs-var">tvs'</span></a></span><span class="hs-special">,</span><span> </span><span class="annot"><span class="annottext">ThetaType
</span><a href="#local-6989586621681174403"><span class="hs-identifier hs-var">inst_tys'</span></a></span><span class="hs-special">)</span><span>
</span><span id="line-209"></span><span>
</span><span id="line-210"></span><span>           </span><span id="local-6989586621681174396"><span class="annot"><span class="annottext">is_generic :: Bool
</span><a href="#local-6989586621681174396"><span class="hs-identifier hs-var hs-var">is_generic</span></a></span></span><span>  </span><span class="hs-glyph">=</span><span> </span><span class="annot"><span class="annottext">Class
</span><a href="#local-6989586621681174462"><span class="hs-identifier hs-var">main_cls</span></a></span><span> </span><span class="annot"><span class="annottext">Class -&gt; Unique -&gt; Bool
forall a. Uniquable a =&gt; a -&gt; Unique -&gt; Bool
</span><a href="GHC.Types.Unique.html#hasKey"><span class="hs-operator hs-var">`hasKey`</span></a></span><span> </span><span class="annot"><span class="annottext">Unique
</span><a href="GHC.Builtin.Names.html#genClassKey"><span class="hs-identifier hs-var">genClassKey</span></a></span><span>
</span><span id="line-211"></span><span>           </span><span id="local-6989586621681174392"><span class="annot"><span class="annottext">is_generic1 :: Bool
</span><a href="#local-6989586621681174392"><span class="hs-identifier hs-var hs-var">is_generic1</span></a></span></span><span> </span><span class="hs-glyph">=</span><span> </span><span class="annot"><span class="annottext">Class
</span><a href="#local-6989586621681174462"><span class="hs-identifier hs-var">main_cls</span></a></span><span> </span><span class="annot"><span class="annottext">Class -&gt; Unique -&gt; Bool
forall a. Uniquable a =&gt; a -&gt; Unique -&gt; Bool
</span><a href="GHC.Types.Unique.html#hasKey"><span class="hs-operator hs-var">`hasKey`</span></a></span><span> </span><span class="annot"><span class="annottext">Unique
</span><a href="GHC.Builtin.Names.html#gen1ClassKey"><span class="hs-identifier hs-var">gen1ClassKey</span></a></span><span>
</span><span id="line-212"></span><span>           </span><span class="hs-comment">-- is_functor_like: see Note [Inferring the instance context]</span><span>
</span><span id="line-213"></span><span>           </span><span id="local-6989586621681174388"><span class="annot"><span class="annottext">is_functor_like :: Bool
</span><a href="#local-6989586621681174388"><span class="hs-identifier hs-var hs-var">is_functor_like</span></a></span></span><span> </span><span class="hs-glyph">=</span><span> </span><span class="annot"><span class="annottext">HasDebugCallStack =&gt; PredType -&gt; PredType
PredType -&gt; PredType
</span><a href="GHC.Core.Type.html#tcTypeKind"><span class="hs-identifier hs-var">tcTypeKind</span></a></span><span> </span><span class="annot"><span class="annottext">PredType
</span><a href="#local-6989586621681174459"><span class="hs-identifier hs-var">inst_ty</span></a></span><span> </span><span class="annot"><span class="annottext">HasDebugCallStack =&gt; PredType -&gt; PredType -&gt; Bool
PredType -&gt; PredType -&gt; Bool
</span><a href="GHC.Tc.Utils.TcType.html#tcEqKind"><span class="hs-operator hs-var">`tcEqKind`</span></a></span><span> </span><span class="annot"><span class="annottext">PredType
</span><a href="GHC.Builtin.Types.html#typeToTypeKind"><span class="hs-identifier hs-var">typeToTypeKind</span></a></span><span>
</span><span id="line-214"></span><span>                          </span><span class="annot"><span class="annottext">Bool -&gt; Bool -&gt; Bool
</span><span class="hs-operator hs-var">||</span></span><span> </span><span class="annot"><span class="annottext">Bool
</span><a href="#local-6989586621681174392"><span class="hs-identifier hs-var">is_generic1</span></a></span><span>
</span><span id="line-215"></span><span>
</span><span id="line-216"></span><span>           </span><span class="annot"><a href="#local-6989586621681174384"><span class="hs-identifier hs-type">get_gen1_constraints</span></a></span><span> </span><span class="hs-glyph">::</span><span> </span><span class="annot"><a href="GHC.Core.Class.html#Class"><span class="hs-identifier hs-type">Class</span></a></span><span> </span><span class="hs-glyph">-&gt;</span><span> </span><span class="annot"><a href="GHC.Tc.Types.Origin.html#CtOrigin"><span class="hs-identifier hs-type">CtOrigin</span></a></span><span> </span><span class="hs-glyph">-&gt;</span><span> </span><span class="annot"><a href="GHC.Types.Basic.html#TypeOrKind"><span class="hs-identifier hs-type">TypeOrKind</span></a></span><span> </span><span class="hs-glyph">-&gt;</span><span> </span><span class="annot"><a href="GHC.Core.TyCo.Rep.html#Type"><span class="hs-identifier hs-type">Type</span></a></span><span>
</span><span id="line-217"></span><span>                                </span><span class="hs-glyph">-&gt;</span><span> </span><span class="hs-special">[</span><span class="hs-special">(</span><span class="hs-special">[</span><span class="annot"><a href="GHC.Tc.Deriv.Utils.html#PredOrigin"><span class="hs-identifier hs-type">PredOrigin</span></a></span><span class="hs-special">]</span><span class="hs-special">,</span><span> </span><span class="annot"><a href="../../base/src/GHC.Maybe.html#Maybe"><span class="hs-identifier hs-type">Maybe</span></a></span><span> </span><span class="annot"><a href="GHC.Core.TyCo.Subst.html#TCvSubst"><span class="hs-identifier hs-type">TCvSubst</span></a></span><span class="hs-special">)</span><span class="hs-special">]</span><span>
</span><span id="line-218"></span><span>           </span><span id="local-6989586621681174384"><span class="annot"><span class="annottext">get_gen1_constraints :: Class
-&gt; CtOrigin
-&gt; TypeOrKind
-&gt; PredType
-&gt; [([PredOrigin], Maybe TCvSubst)]
</span><a href="#local-6989586621681174384"><span class="hs-identifier hs-var hs-var">get_gen1_constraints</span></a></span></span><span> </span><span id="local-6989586621681174383"><span class="annot"><span class="annottext">Class
</span><a href="#local-6989586621681174383"><span class="hs-identifier hs-var">functor_cls</span></a></span></span><span> </span><span id="local-6989586621681174382"><span class="annot"><span class="annottext">CtOrigin
</span><a href="#local-6989586621681174382"><span class="hs-identifier hs-var">orig</span></a></span></span><span> </span><span id="local-6989586621681174381"><span class="annot"><span class="annottext">TypeOrKind
</span><a href="#local-6989586621681174381"><span class="hs-identifier hs-var">t_or_k</span></a></span></span><span> </span><span id="local-6989586621681174380"><span class="annot"><span class="annottext">PredType
</span><a href="#local-6989586621681174380"><span class="hs-identifier hs-var">ty</span></a></span></span><span>
</span><span id="line-219"></span><span>              </span><span class="hs-glyph">=</span><span> </span><span class="annot"><span class="annottext">CtOrigin
-&gt; TypeOrKind
-&gt; Class
-&gt; ThetaType
-&gt; [([PredOrigin], Maybe TCvSubst)]
</span><a href="#local-6989586621681174379"><span class="hs-identifier hs-var">mk_functor_like_constraints</span></a></span><span> </span><span class="annot"><span class="annottext">CtOrigin
</span><a href="#local-6989586621681174382"><span class="hs-identifier hs-var">orig</span></a></span><span> </span><span class="annot"><span class="annottext">TypeOrKind
</span><a href="#local-6989586621681174381"><span class="hs-identifier hs-var">t_or_k</span></a></span><span> </span><span class="annot"><span class="annottext">Class
</span><a href="#local-6989586621681174383"><span class="hs-identifier hs-var">functor_cls</span></a></span><span> </span><span class="annot"><span class="annottext">(ThetaType -&gt; [([PredOrigin], Maybe TCvSubst)])
-&gt; ThetaType -&gt; [([PredOrigin], Maybe TCvSubst)]
forall a b. (a -&gt; b) -&gt; a -&gt; b
</span><a href="../../base/src/GHC.Base.html#%24"><span class="hs-operator hs-var">$</span></a></span><span>
</span><span id="line-220"></span><span>                </span><span class="annot"><span class="annottext">TcTyVar -&gt; PredType -&gt; ThetaType
</span><a href="GHC.Tc.Deriv.Generics.html#get_gen1_constrained_tys"><span class="hs-identifier hs-var">get_gen1_constrained_tys</span></a></span><span> </span><span class="annot"><span class="annottext">TcTyVar
</span><a href="#local-6989586621681174377"><span class="hs-identifier hs-var">last_tv</span></a></span><span> </span><span class="annot"><span class="annottext">PredType
</span><a href="#local-6989586621681174380"><span class="hs-identifier hs-var">ty</span></a></span><span>
</span><span id="line-221"></span><span>
</span><span id="line-222"></span><span>           </span><span class="annot"><a href="#local-6989586621681174376"><span class="hs-identifier hs-type">get_std_constrained_tys</span></a></span><span> </span><span class="hs-glyph">::</span><span> </span><span class="annot"><a href="GHC.Tc.Types.Origin.html#CtOrigin"><span class="hs-identifier hs-type">CtOrigin</span></a></span><span> </span><span class="hs-glyph">-&gt;</span><span> </span><span class="annot"><a href="GHC.Types.Basic.html#TypeOrKind"><span class="hs-identifier hs-type">TypeOrKind</span></a></span><span> </span><span class="hs-glyph">-&gt;</span><span> </span><span class="annot"><a href="GHC.Core.TyCo.Rep.html#Type"><span class="hs-identifier hs-type">Type</span></a></span><span>
</span><span id="line-223"></span><span>                                   </span><span class="hs-glyph">-&gt;</span><span> </span><span class="hs-special">[</span><span class="hs-special">(</span><span class="hs-special">[</span><span class="annot"><a href="GHC.Tc.Deriv.Utils.html#PredOrigin"><span class="hs-identifier hs-type">PredOrigin</span></a></span><span class="hs-special">]</span><span class="hs-special">,</span><span> </span><span class="annot"><a href="../../base/src/GHC.Maybe.html#Maybe"><span class="hs-identifier hs-type">Maybe</span></a></span><span> </span><span class="annot"><a href="GHC.Core.TyCo.Subst.html#TCvSubst"><span class="hs-identifier hs-type">TCvSubst</span></a></span><span class="hs-special">)</span><span class="hs-special">]</span><span>
</span><span id="line-224"></span><span>           </span><span id="local-6989586621681174376"><span class="annot"><span class="annottext">get_std_constrained_tys :: CtOrigin
-&gt; TypeOrKind -&gt; PredType -&gt; [([PredOrigin], Maybe TCvSubst)]
</span><a href="#local-6989586621681174376"><span class="hs-identifier hs-var hs-var">get_std_constrained_tys</span></a></span></span><span> </span><span id="local-6989586621681174375"><span class="annot"><span class="annottext">CtOrigin
</span><a href="#local-6989586621681174375"><span class="hs-identifier hs-var">orig</span></a></span></span><span> </span><span id="local-6989586621681174374"><span class="annot"><span class="annottext">TypeOrKind
</span><a href="#local-6989586621681174374"><span class="hs-identifier hs-var">t_or_k</span></a></span></span><span> </span><span id="local-6989586621681174373"><span class="annot"><span class="annottext">PredType
</span><a href="#local-6989586621681174373"><span class="hs-identifier hs-var">ty</span></a></span></span><span>
</span><span id="line-225"></span><span>               </span><span class="hs-glyph">|</span><span> </span><span class="annot"><span class="annottext">Bool
</span><a href="#local-6989586621681174388"><span class="hs-identifier hs-var">is_functor_like</span></a></span><span>
</span><span id="line-226"></span><span>               </span><span class="hs-glyph">=</span><span> </span><span class="annot"><span class="annottext">CtOrigin
-&gt; TypeOrKind
-&gt; Class
-&gt; ThetaType
-&gt; [([PredOrigin], Maybe TCvSubst)]
</span><a href="#local-6989586621681174379"><span class="hs-identifier hs-var">mk_functor_like_constraints</span></a></span><span> </span><span class="annot"><span class="annottext">CtOrigin
</span><a href="#local-6989586621681174375"><span class="hs-identifier hs-var">orig</span></a></span><span> </span><span class="annot"><span class="annottext">TypeOrKind
</span><a href="#local-6989586621681174374"><span class="hs-identifier hs-var">t_or_k</span></a></span><span> </span><span class="annot"><span class="annottext">Class
</span><a href="#local-6989586621681174462"><span class="hs-identifier hs-var">main_cls</span></a></span><span> </span><span class="annot"><span class="annottext">(ThetaType -&gt; [([PredOrigin], Maybe TCvSubst)])
-&gt; ThetaType -&gt; [([PredOrigin], Maybe TCvSubst)]
forall a b. (a -&gt; b) -&gt; a -&gt; b
</span><a href="../../base/src/GHC.Base.html#%24"><span class="hs-operator hs-var">$</span></a></span><span>
</span><span id="line-227"></span><span>                 </span><span class="annot"><span class="annottext">TcTyVar -&gt; PredType -&gt; ThetaType
</span><a href="GHC.Tc.Deriv.Functor.html#deepSubtypesContaining"><span class="hs-identifier hs-var">deepSubtypesContaining</span></a></span><span> </span><span class="annot"><span class="annottext">TcTyVar
</span><a href="#local-6989586621681174377"><span class="hs-identifier hs-var">last_tv</span></a></span><span> </span><span class="annot"><span class="annottext">PredType
</span><a href="#local-6989586621681174373"><span class="hs-identifier hs-var">ty</span></a></span><span>
</span><span id="line-228"></span><span>               </span><span class="hs-glyph">|</span><span> </span><span class="annot"><span class="annottext">Bool
</span><a href="../../base/src/GHC.Base.html#otherwise"><span class="hs-identifier hs-var">otherwise</span></a></span><span>
</span><span id="line-229"></span><span>               </span><span class="hs-glyph">=</span><span> </span><span class="hs-special">[</span><span class="hs-special">(</span><span> </span><span class="hs-special">[</span><span class="annot"><span class="annottext">CtOrigin -&gt; TypeOrKind -&gt; Class -&gt; PredType -&gt; PredOrigin
</span><a href="#local-6989586621681174371"><span class="hs-identifier hs-var">mk_cls_pred</span></a></span><span> </span><span class="annot"><span class="annottext">CtOrigin
</span><a href="#local-6989586621681174375"><span class="hs-identifier hs-var">orig</span></a></span><span> </span><span class="annot"><span class="annottext">TypeOrKind
</span><a href="#local-6989586621681174374"><span class="hs-identifier hs-var">t_or_k</span></a></span><span> </span><span class="annot"><span class="annottext">Class
</span><a href="#local-6989586621681174462"><span class="hs-identifier hs-var">main_cls</span></a></span><span> </span><span class="annot"><span class="annottext">PredType
</span><a href="#local-6989586621681174373"><span class="hs-identifier hs-var">ty</span></a></span><span class="hs-special">]</span><span>
</span><span id="line-230"></span><span>                  </span><span class="hs-special">,</span><span> </span><span class="annot"><span class="annottext">Maybe TCvSubst
forall a. Maybe a
</span><a href="../../base/src/GHC.Maybe.html#Nothing"><span class="hs-identifier hs-var">Nothing</span></a></span><span> </span><span class="hs-special">)</span><span class="hs-special">]</span><span>
</span><span id="line-231"></span><span>
</span><span id="line-232"></span><span>           </span><span class="annot"><a href="#local-6989586621681174379"><span class="hs-identifier hs-type">mk_functor_like_constraints</span></a></span><span> </span><span class="hs-glyph">::</span><span> </span><span class="annot"><a href="GHC.Tc.Types.Origin.html#CtOrigin"><span class="hs-identifier hs-type">CtOrigin</span></a></span><span> </span><span class="hs-glyph">-&gt;</span><span> </span><span class="annot"><a href="GHC.Types.Basic.html#TypeOrKind"><span class="hs-identifier hs-type">TypeOrKind</span></a></span><span>
</span><span id="line-233"></span><span>                                       </span><span class="hs-glyph">-&gt;</span><span> </span><span class="annot"><a href="GHC.Core.Class.html#Class"><span class="hs-identifier hs-type">Class</span></a></span><span> </span><span class="hs-glyph">-&gt;</span><span> </span><span class="hs-special">[</span><span class="annot"><a href="GHC.Core.TyCo.Rep.html#Type"><span class="hs-identifier hs-type">Type</span></a></span><span class="hs-special">]</span><span>
</span><span id="line-234"></span><span>                                       </span><span class="hs-glyph">-&gt;</span><span> </span><span class="hs-special">[</span><span class="hs-special">(</span><span class="hs-special">[</span><span class="annot"><a href="GHC.Tc.Deriv.Utils.html#PredOrigin"><span class="hs-identifier hs-type">PredOrigin</span></a></span><span class="hs-special">]</span><span class="hs-special">,</span><span> </span><span class="annot"><a href="../../base/src/GHC.Maybe.html#Maybe"><span class="hs-identifier hs-type">Maybe</span></a></span><span> </span><span class="annot"><a href="GHC.Core.TyCo.Subst.html#TCvSubst"><span class="hs-identifier hs-type">TCvSubst</span></a></span><span class="hs-special">)</span><span class="hs-special">]</span><span>
</span><span id="line-235"></span><span>           </span><span class="hs-comment">-- 'cls' is usually main_cls (Functor or Traversable etc), but if</span><span>
</span><span id="line-236"></span><span>           </span><span class="hs-comment">-- main_cls = Generic1, then 'cls' can be Functor; see</span><span>
</span><span id="line-237"></span><span>           </span><span class="hs-comment">-- get_gen1_constraints</span><span>
</span><span id="line-238"></span><span>           </span><span class="hs-comment">--</span><span>
</span><span id="line-239"></span><span>           </span><span class="hs-comment">-- For each type, generate two constraints,</span><span>
</span><span id="line-240"></span><span>           </span><span class="hs-comment">-- [cls ty, kind(ty) ~ (*-&gt;*)], and a kind substitution that results</span><span>
</span><span id="line-241"></span><span>           </span><span class="hs-comment">-- from unifying  kind(ty) with * -&gt; *. If the unification is</span><span>
</span><span id="line-242"></span><span>           </span><span class="hs-comment">-- successful, it will ensure that the resulting instance is well</span><span>
</span><span id="line-243"></span><span>           </span><span class="hs-comment">-- kinded. If not, the second constraint will result in an error</span><span>
</span><span id="line-244"></span><span>           </span><span class="hs-comment">-- message which points out the kind mismatch.</span><span>
</span><span id="line-245"></span><span>           </span><span class="hs-comment">-- See Note [Inferring the instance context]</span><span>
</span><span id="line-246"></span><span>           </span><span id="local-6989586621681174379"><span class="annot"><span class="annottext">mk_functor_like_constraints :: CtOrigin
-&gt; TypeOrKind
-&gt; Class
-&gt; ThetaType
-&gt; [([PredOrigin], Maybe TCvSubst)]
</span><a href="#local-6989586621681174379"><span class="hs-identifier hs-var hs-var">mk_functor_like_constraints</span></a></span></span><span> </span><span id="local-6989586621681174370"><span class="annot"><span class="annottext">CtOrigin
</span><a href="#local-6989586621681174370"><span class="hs-identifier hs-var">orig</span></a></span></span><span> </span><span id="local-6989586621681174369"><span class="annot"><span class="annottext">TypeOrKind
</span><a href="#local-6989586621681174369"><span class="hs-identifier hs-var">t_or_k</span></a></span></span><span> </span><span id="local-6989586621681174368"><span class="annot"><span class="annottext">Class
</span><a href="#local-6989586621681174368"><span class="hs-identifier hs-var">cls</span></a></span></span><span>
</span><span id="line-247"></span><span>              </span><span class="hs-glyph">=</span><span> </span><span class="annot"><span class="annottext">(PredType -&gt; ([PredOrigin], Maybe TCvSubst))
-&gt; ThetaType -&gt; [([PredOrigin], Maybe TCvSubst)]
forall a b. (a -&gt; b) -&gt; [a] -&gt; [b]
</span><a href="../../base/src/GHC.Base.html#map"><span class="hs-identifier hs-var">map</span></a></span><span> </span><span class="annot"><span class="annottext">((PredType -&gt; ([PredOrigin], Maybe TCvSubst))
 -&gt; ThetaType -&gt; [([PredOrigin], Maybe TCvSubst)])
-&gt; (PredType -&gt; ([PredOrigin], Maybe TCvSubst))
-&gt; ThetaType
-&gt; [([PredOrigin], Maybe TCvSubst)]
forall a b. (a -&gt; b) -&gt; a -&gt; b
</span><a href="../../base/src/GHC.Base.html#%24"><span class="hs-operator hs-var">$</span></a></span><span> </span><span class="hs-glyph">\</span><span id="local-6989586621681174367"><span class="annot"><span class="annottext">PredType
</span><a href="#local-6989586621681174367"><span class="hs-identifier hs-var">ty</span></a></span></span><span> </span><span class="hs-glyph">-&gt;</span><span> </span><span class="hs-keyword">let</span><span> </span><span id="local-6989586621681174365"><span class="annot"><span class="annottext">ki :: PredType
</span><a href="#local-6989586621681174365"><span class="hs-identifier hs-var hs-var">ki</span></a></span></span><span> </span><span class="hs-glyph">=</span><span> </span><span class="annot"><span class="annottext">HasDebugCallStack =&gt; PredType -&gt; PredType
PredType -&gt; PredType
</span><a href="GHC.Core.Type.html#tcTypeKind"><span class="hs-identifier hs-var">tcTypeKind</span></a></span><span> </span><span class="annot"><span class="annottext">PredType
</span><a href="#local-6989586621681174367"><span class="hs-identifier hs-var">ty</span></a></span><span> </span><span class="hs-keyword">in</span><span>
</span><span id="line-248"></span><span>                             </span><span class="hs-special">(</span><span> </span><span class="hs-special">[</span><span> </span><span class="annot"><span class="annottext">CtOrigin -&gt; TypeOrKind -&gt; Class -&gt; PredType -&gt; PredOrigin
</span><a href="#local-6989586621681174371"><span class="hs-identifier hs-var">mk_cls_pred</span></a></span><span> </span><span class="annot"><span class="annottext">CtOrigin
</span><a href="#local-6989586621681174370"><span class="hs-identifier hs-var">orig</span></a></span><span> </span><span class="annot"><span class="annottext">TypeOrKind
</span><a href="#local-6989586621681174369"><span class="hs-identifier hs-var">t_or_k</span></a></span><span> </span><span class="annot"><span class="annottext">Class
</span><a href="#local-6989586621681174368"><span class="hs-identifier hs-var">cls</span></a></span><span> </span><span class="annot"><span class="annottext">PredType
</span><a href="#local-6989586621681174367"><span class="hs-identifier hs-var">ty</span></a></span><span>
</span><span id="line-249"></span><span>                               </span><span class="hs-special">,</span><span> </span><span class="annot"><span class="annottext">CtOrigin -&gt; TypeOrKind -&gt; PredType -&gt; PredOrigin
</span><a href="GHC.Tc.Deriv.Utils.html#mkPredOrigin"><span class="hs-identifier hs-var">mkPredOrigin</span></a></span><span> </span><span class="annot"><span class="annottext">CtOrigin
</span><a href="#local-6989586621681174370"><span class="hs-identifier hs-var">orig</span></a></span><span> </span><span class="annot"><span class="annottext">TypeOrKind
</span><a href="GHC.Types.Basic.html#KindLevel"><span class="hs-identifier hs-var">KindLevel</span></a></span><span>
</span><span id="line-250"></span><span>                                   </span><span class="hs-special">(</span><span class="annot"><span class="annottext">PredType -&gt; PredType -&gt; PredType
</span><a href="GHC.Core.Coercion.html#mkPrimEqPred"><span class="hs-identifier hs-var">mkPrimEqPred</span></a></span><span> </span><span class="annot"><span class="annottext">PredType
</span><a href="#local-6989586621681174365"><span class="hs-identifier hs-var">ki</span></a></span><span> </span><span class="annot"><span class="annottext">PredType
</span><a href="GHC.Builtin.Types.html#typeToTypeKind"><span class="hs-identifier hs-var">typeToTypeKind</span></a></span><span class="hs-special">)</span><span> </span><span class="hs-special">]</span><span>
</span><span id="line-251"></span><span>                             </span><span class="hs-special">,</span><span> </span><span class="annot"><span class="annottext">PredType -&gt; PredType -&gt; Maybe TCvSubst
</span><a href="GHC.Core.Unify.html#tcUnifyTy"><span class="hs-identifier hs-var">tcUnifyTy</span></a></span><span> </span><span class="annot"><span class="annottext">PredType
</span><a href="#local-6989586621681174365"><span class="hs-identifier hs-var">ki</span></a></span><span> </span><span class="annot"><span class="annottext">PredType
</span><a href="GHC.Builtin.Types.html#typeToTypeKind"><span class="hs-identifier hs-var">typeToTypeKind</span></a></span><span>
</span><span id="line-252"></span><span>                             </span><span class="hs-special">)</span><span>
</span><span id="line-253"></span><span>
</span><span id="line-254"></span><span>           </span><span id="local-6989586621681174362"><span class="annot"><span class="annottext">rep_tc_tvs :: [TcTyVar]
</span><a href="#local-6989586621681174362"><span class="hs-identifier hs-var hs-var">rep_tc_tvs</span></a></span></span><span>      </span><span class="hs-glyph">=</span><span> </span><span class="annot"><span class="annottext">TyCon -&gt; [TcTyVar]
</span><a href="GHC.Core.TyCon.html#tyConTyVars"><span class="hs-identifier hs-var hs-var">tyConTyVars</span></a></span><span> </span><span class="annot"><span class="annottext">TyCon
</span><a href="#local-6989586621681174466"><span class="hs-identifier hs-var">rep_tc</span></a></span><span>
</span><span id="line-255"></span><span>           </span><span id="local-6989586621681174377"><span class="annot"><span class="annottext">last_tv :: TcTyVar
</span><a href="#local-6989586621681174377"><span class="hs-identifier hs-var hs-var">last_tv</span></a></span></span><span>         </span><span class="hs-glyph">=</span><span> </span><span class="annot"><span class="annottext">[TcTyVar] -&gt; TcTyVar
forall a. [a] -&gt; a
</span><a href="../../base/src/GHC.List.html#last"><span class="hs-identifier hs-var">last</span></a></span><span> </span><span class="annot"><span class="annottext">[TcTyVar]
</span><a href="#local-6989586621681174362"><span class="hs-identifier hs-var">rep_tc_tvs</span></a></span><span>
</span><span id="line-256"></span><span>           </span><span class="hs-comment">-- When we first gather up the constraints to solve, most of them</span><span>
</span><span id="line-257"></span><span>           </span><span class="hs-comment">-- contain rep_tc_tvs, i.e., the type variables from the derived</span><span>
</span><span id="line-258"></span><span>           </span><span class="hs-comment">-- datatype's type constructor. We don't want these type variables</span><span>
</span><span id="line-259"></span><span>           </span><span class="hs-comment">-- to appear in the final instance declaration, so we must</span><span>
</span><span id="line-260"></span><span>           </span><span class="hs-comment">-- substitute each type variable with its counterpart in the derived</span><span>
</span><span id="line-261"></span><span>           </span><span class="hs-comment">-- instance. rep_tc_args lists each of these counterpart types in</span><span>
</span><span id="line-262"></span><span>           </span><span class="hs-comment">-- the same order as the type variables.</span><span>
</span><span id="line-263"></span><span>           </span><span id="local-6989586621681174431"><span class="annot"><span class="annottext">all_rep_tc_args :: ThetaType
</span><a href="#local-6989586621681174431"><span class="hs-identifier hs-var hs-var">all_rep_tc_args</span></a></span></span><span>
</span><span id="line-264"></span><span>             </span><span class="hs-glyph">=</span><span> </span><span class="annot"><span class="annottext">ThetaType
</span><a href="#local-6989586621681174464"><span class="hs-identifier hs-var">rep_tc_args</span></a></span><span> </span><span class="annot"><span class="annottext">ThetaType -&gt; ThetaType -&gt; ThetaType
forall a. [a] -&gt; [a] -&gt; [a]
</span><a href="../../base/src/GHC.Base.html#%2B%2B"><span class="hs-operator hs-var">++</span></a></span><span> </span><span class="annot"><span class="annottext">(TcTyVar -&gt; PredType) -&gt; [TcTyVar] -&gt; ThetaType
forall a b. (a -&gt; b) -&gt; [a] -&gt; [b]
</span><a href="../../base/src/GHC.Base.html#map"><span class="hs-identifier hs-var">map</span></a></span><span> </span><span class="annot"><span class="annottext">TcTyVar -&gt; PredType
</span><a href="GHC.Core.TyCo.Rep.html#mkTyVarTy"><span class="hs-identifier hs-var">mkTyVarTy</span></a></span><span>
</span><span id="line-265"></span><span>                                  </span><span class="hs-special">(</span><span class="annot"><span class="annottext">Int -&gt; [TcTyVar] -&gt; [TcTyVar]
forall a. Int -&gt; [a] -&gt; [a]
</span><a href="../../base/src/GHC.List.html#drop"><span class="hs-identifier hs-var">drop</span></a></span><span> </span><span class="hs-special">(</span><span class="annot"><span class="annottext">ThetaType -&gt; Int
forall (t :: * -&gt; *) a. Foldable t =&gt; t a -&gt; Int
</span><a href="../../base/src/Data.Foldable.html#length"><span class="hs-identifier hs-var">length</span></a></span><span> </span><span class="annot"><span class="annottext">ThetaType
</span><a href="#local-6989586621681174464"><span class="hs-identifier hs-var">rep_tc_args</span></a></span><span class="hs-special">)</span><span> </span><span class="annot"><span class="annottext">[TcTyVar]
</span><a href="#local-6989586621681174362"><span class="hs-identifier hs-var">rep_tc_tvs</span></a></span><span class="hs-special">)</span><span>
</span><span id="line-266"></span><span>
</span><span id="line-267"></span><span>               </span><span class="hs-comment">-- Stupid constraints</span><span>
</span><span id="line-268"></span><span>           </span><span id="local-6989586621681174353"><span class="annot"><span class="annottext">stupid_constraints :: [ThetaOrigin]
</span><a href="#local-6989586621681174353"><span class="hs-identifier hs-var hs-var">stupid_constraints</span></a></span></span><span>
</span><span id="line-269"></span><span>             </span><span class="hs-glyph">=</span><span> </span><span class="hs-special">[</span><span> </span><span class="annot"><span class="annottext">CtOrigin
-&gt; TypeOrKind
-&gt; [TcTyVar]
-&gt; [TcTyVar]
-&gt; ThetaType
-&gt; ThetaType
-&gt; ThetaOrigin
</span><a href="GHC.Tc.Deriv.Utils.html#mkThetaOrigin"><span class="hs-identifier hs-var">mkThetaOrigin</span></a></span><span> </span><span class="annot"><span class="annottext">CtOrigin
</span><a href="#local-6989586621681174352"><span class="hs-identifier hs-var">deriv_origin</span></a></span><span> </span><span class="annot"><span class="annottext">TypeOrKind
</span><a href="GHC.Types.Basic.html#TypeLevel"><span class="hs-identifier hs-var">TypeLevel</span></a></span><span> </span><span class="hs-special">[</span><span class="hs-special">]</span><span> </span><span class="hs-special">[</span><span class="hs-special">]</span><span> </span><span class="hs-special">[</span><span class="hs-special">]</span><span> </span><span class="annot"><span class="annottext">(ThetaType -&gt; ThetaOrigin) -&gt; ThetaType -&gt; ThetaOrigin
forall a b. (a -&gt; b) -&gt; a -&gt; b
</span><a href="../../base/src/GHC.Base.html#%24"><span class="hs-operator hs-var">$</span></a></span><span>
</span><span id="line-270"></span><span>                 </span><span class="annot"><span class="annottext">HasCallStack =&gt; TCvSubst -&gt; ThetaType -&gt; ThetaType
TCvSubst -&gt; ThetaType -&gt; ThetaType
</span><a href="GHC.Core.TyCo.Subst.html#substTheta"><span class="hs-identifier hs-var">substTheta</span></a></span><span> </span><span class="annot"><span class="annottext">TCvSubst
</span><a href="#local-6989586621681174351"><span class="hs-identifier hs-var">tc_subst</span></a></span><span> </span><span class="hs-special">(</span><span class="annot"><span class="annottext">TyCon -&gt; ThetaType
</span><a href="GHC.Core.TyCon.html#tyConStupidTheta"><span class="hs-identifier hs-var">tyConStupidTheta</span></a></span><span> </span><span class="annot"><span class="annottext">TyCon
</span><a href="#local-6989586621681174466"><span class="hs-identifier hs-var">rep_tc</span></a></span><span class="hs-special">)</span><span> </span><span class="hs-special">]</span><span>
</span><span id="line-271"></span><span>           </span><span id="local-6989586621681174351"><span class="annot"><span class="annottext">tc_subst :: TCvSubst
</span><a href="#local-6989586621681174351"><span class="hs-identifier hs-var hs-var">tc_subst</span></a></span></span><span> </span><span class="hs-glyph">=</span><span> </span><span class="hs-comment">-- See the comment with all_rep_tc_args for an</span><span>
</span><span id="line-272"></span><span>                      </span><span class="hs-comment">-- explanation of this assertion</span><span>
</span><span id="line-273"></span><span>                      </span><span class="hs-identifier">ASSERT</span><span class="hs-special">(</span><span> </span><span class="hs-identifier">equalLength</span><span> </span><span class="hs-identifier">rep_tc_tvs</span><span> </span><span class="hs-identifier">all_rep_tc_args</span><span> </span><span class="hs-special">)</span><span>
</span><span id="line-274"></span><span>                      </span><span class="annot"><span class="annottext">[TcTyVar] -&gt; ThetaType -&gt; TCvSubst
HasDebugCallStack =&gt; [TcTyVar] -&gt; ThetaType -&gt; TCvSubst
</span><a href="GHC.Core.TyCo.Subst.html#zipTvSubst"><span class="hs-identifier hs-var">zipTvSubst</span></a></span><span> </span><span class="annot"><span class="annottext">[TcTyVar]
</span><a href="#local-6989586621681174362"><span class="hs-identifier hs-var">rep_tc_tvs</span></a></span><span> </span><span class="annot"><span class="annottext">ThetaType
</span><a href="#local-6989586621681174431"><span class="hs-identifier hs-var">all_rep_tc_args</span></a></span><span>
</span><span id="line-275"></span><span>
</span><span id="line-276"></span><span>           </span><span class="hs-comment">-- Extra Data constraints</span><span>
</span><span id="line-277"></span><span>           </span><span class="hs-comment">-- The Data class (only) requires that for</span><span>
</span><span id="line-278"></span><span>           </span><span class="hs-comment">--    instance (...) =&gt; Data (T t1 t2)</span><span>
</span><span id="line-279"></span><span>           </span><span class="hs-comment">-- IF   t1:*, t2:*</span><span>
</span><span id="line-280"></span><span>           </span><span class="hs-comment">-- THEN (Data t1, Data t2) are among the (...) constraints</span><span>
</span><span id="line-281"></span><span>           </span><span class="hs-comment">-- Reason: when the IF holds, we generate a method</span><span>
</span><span id="line-282"></span><span>           </span><span class="hs-comment">--             dataCast2 f = gcast2 f</span><span>
</span><span id="line-283"></span><span>           </span><span class="hs-comment">--         and we need the Data constraints to typecheck the method</span><span>
</span><span id="line-284"></span><span>           </span><span id="local-6989586621681174348"><span class="annot"><span class="annottext">extra_constraints :: [ThetaOrigin]
</span><a href="#local-6989586621681174348"><span class="hs-identifier hs-var hs-var">extra_constraints</span></a></span></span><span> </span><span class="hs-glyph">=</span><span> </span><span class="hs-special">[</span><span class="annot"><span class="annottext">[PredOrigin] -&gt; ThetaOrigin
</span><a href="GHC.Tc.Deriv.Utils.html#mkThetaOriginFromPreds"><span class="hs-identifier hs-var">mkThetaOriginFromPreds</span></a></span><span> </span><span class="annot"><span class="annottext">[PredOrigin]
</span><a href="#local-6989586621681174347"><span class="hs-identifier hs-var">constrs</span></a></span><span class="hs-special">]</span><span>
</span><span id="line-285"></span><span>             </span><span class="hs-keyword">where</span><span>
</span><span id="line-286"></span><span>               </span><span id="local-6989586621681174347"><span class="annot"><span class="annottext">constrs :: [PredOrigin]
</span><a href="#local-6989586621681174347"><span class="hs-identifier hs-var hs-var">constrs</span></a></span></span><span>
</span><span id="line-287"></span><span>                 </span><span class="hs-glyph">|</span><span> </span><span class="annot"><span class="annottext">Class
</span><a href="#local-6989586621681174462"><span class="hs-identifier hs-var">main_cls</span></a></span><span> </span><span class="annot"><span class="annottext">Class -&gt; Unique -&gt; Bool
forall a. Uniquable a =&gt; a -&gt; Unique -&gt; Bool
</span><a href="GHC.Types.Unique.html#hasKey"><span class="hs-operator hs-var">`hasKey`</span></a></span><span> </span><span class="annot"><span class="annottext">Unique
</span><a href="GHC.Builtin.Names.html#dataClassKey"><span class="hs-identifier hs-var">dataClassKey</span></a></span><span>
</span><span id="line-288"></span><span>                 </span><span class="hs-special">,</span><span> </span><span class="annot"><span class="annottext">(PredType -&gt; Bool) -&gt; ThetaType -&gt; Bool
forall (t :: * -&gt; *) a. Foldable t =&gt; (a -&gt; Bool) -&gt; t a -&gt; Bool
</span><a href="../../base/src/Data.Foldable.html#all"><span class="hs-identifier hs-var">all</span></a></span><span> </span><span class="hs-special">(</span><span class="annot"><span class="annottext">PredType -&gt; Bool
</span><a href="GHC.Core.Type.html#isLiftedTypeKind"><span class="hs-identifier hs-var">isLiftedTypeKind</span></a></span><span> </span><span class="annot"><span class="annottext">(PredType -&gt; Bool) -&gt; (PredType -&gt; PredType) -&gt; PredType -&gt; Bool
forall b c a. (b -&gt; c) -&gt; (a -&gt; b) -&gt; a -&gt; c
</span><a href="../../base/src/GHC.Base.html#."><span class="hs-operator hs-var">.</span></a></span><span> </span><span class="annot"><span class="annottext">HasDebugCallStack =&gt; PredType -&gt; PredType
PredType -&gt; PredType
</span><a href="GHC.Core.Type.html#tcTypeKind"><span class="hs-identifier hs-var">tcTypeKind</span></a></span><span class="hs-special">)</span><span> </span><span class="annot"><span class="annottext">ThetaType
</span><a href="#local-6989586621681174464"><span class="hs-identifier hs-var">rep_tc_args</span></a></span><span>
</span><span id="line-289"></span><span>                 </span><span class="hs-glyph">=</span><span> </span><span class="hs-special">[</span><span> </span><span class="annot"><span class="annottext">CtOrigin -&gt; TypeOrKind -&gt; Class -&gt; PredType -&gt; PredOrigin
</span><a href="#local-6989586621681174371"><span class="hs-identifier hs-var">mk_cls_pred</span></a></span><span> </span><span class="annot"><span class="annottext">CtOrigin
</span><a href="#local-6989586621681174352"><span class="hs-identifier hs-var">deriv_origin</span></a></span><span> </span><span class="annot"><span class="annottext">TypeOrKind
</span><a href="#local-6989586621681174339"><span class="hs-identifier hs-var">t_or_k</span></a></span><span> </span><span class="annot"><span class="annottext">Class
</span><a href="#local-6989586621681174462"><span class="hs-identifier hs-var">main_cls</span></a></span><span> </span><span class="annot"><span class="annottext">PredType
</span><a href="#local-6989586621681174338"><span class="hs-identifier hs-var">ty</span></a></span><span>
</span><span id="line-290"></span><span>                   </span><span class="hs-glyph">|</span><span> </span><span class="hs-special">(</span><span id="local-6989586621681174339"><span class="annot"><span class="annottext">TypeOrKind
</span><a href="#local-6989586621681174339"><span class="hs-identifier hs-var">t_or_k</span></a></span></span><span class="hs-special">,</span><span> </span><span id="local-6989586621681174338"><span class="annot"><span class="annottext">PredType
</span><a href="#local-6989586621681174338"><span class="hs-identifier hs-var">ty</span></a></span></span><span class="hs-special">)</span><span> </span><span class="hs-glyph">&lt;-</span><span> </span><span class="annot"><span class="annottext">[TypeOrKind] -&gt; ThetaType -&gt; [(TypeOrKind, PredType)]
forall a b. [a] -&gt; [b] -&gt; [(a, b)]
</span><a href="../../base/src/GHC.List.html#zip"><span class="hs-identifier hs-var">zip</span></a></span><span> </span><span class="annot"><span class="annottext">[TypeOrKind]
</span><a href="#local-6989586621681174451"><span class="hs-identifier hs-var">t_or_ks</span></a></span><span> </span><span class="annot"><span class="annottext">ThetaType
</span><a href="#local-6989586621681174464"><span class="hs-identifier hs-var">rep_tc_args</span></a></span><span class="hs-special">]</span><span>
</span><span id="line-291"></span><span>                 </span><span class="hs-glyph">|</span><span> </span><span class="annot"><span class="annottext">Bool
</span><a href="../../base/src/GHC.Base.html#otherwise"><span class="hs-identifier hs-var">otherwise</span></a></span><span>
</span><span id="line-292"></span><span>                 </span><span class="hs-glyph">=</span><span> </span><span class="hs-special">[</span><span class="hs-special">]</span><span>
</span><span id="line-293"></span><span>
</span><span id="line-294"></span><span>           </span><span id="local-6989586621681174371"><span class="annot"><span class="annottext">mk_cls_pred :: CtOrigin -&gt; TypeOrKind -&gt; Class -&gt; PredType -&gt; PredOrigin
</span><a href="#local-6989586621681174371"><span class="hs-identifier hs-var hs-var">mk_cls_pred</span></a></span></span><span> </span><span id="local-6989586621681174337"><span class="annot"><span class="annottext">CtOrigin
</span><a href="#local-6989586621681174337"><span class="hs-identifier hs-var">orig</span></a></span></span><span> </span><span id="local-6989586621681174336"><span class="annot"><span class="annottext">TypeOrKind
</span><a href="#local-6989586621681174336"><span class="hs-identifier hs-var">t_or_k</span></a></span></span><span> </span><span id="local-6989586621681174335"><span class="annot"><span class="annottext">Class
</span><a href="#local-6989586621681174335"><span class="hs-identifier hs-var">cls</span></a></span></span><span> </span><span id="local-6989586621681174334"><span class="annot"><span class="annottext">PredType
</span><a href="#local-6989586621681174334"><span class="hs-identifier hs-var">ty</span></a></span></span><span>
</span><span id="line-295"></span><span>                </span><span class="hs-comment">-- Don't forget to apply to cls_tys' too</span><span>
</span><span id="line-296"></span><span>              </span><span class="hs-glyph">=</span><span> </span><span class="annot"><span class="annottext">CtOrigin -&gt; TypeOrKind -&gt; PredType -&gt; PredOrigin
</span><a href="GHC.Tc.Deriv.Utils.html#mkPredOrigin"><span class="hs-identifier hs-var">mkPredOrigin</span></a></span><span> </span><span class="annot"><span class="annottext">CtOrigin
</span><a href="#local-6989586621681174337"><span class="hs-identifier hs-var">orig</span></a></span><span> </span><span class="annot"><span class="annottext">TypeOrKind
</span><a href="#local-6989586621681174336"><span class="hs-identifier hs-var">t_or_k</span></a></span><span> </span><span class="hs-special">(</span><span class="annot"><span class="annottext">Class -&gt; ThetaType -&gt; PredType
</span><a href="GHC.Core.Predicate.html#mkClassPred"><span class="hs-identifier hs-var">mkClassPred</span></a></span><span> </span><span class="annot"><span class="annottext">Class
</span><a href="#local-6989586621681174335"><span class="hs-identifier hs-var">cls</span></a></span><span> </span><span class="hs-special">(</span><span class="annot"><span class="annottext">ThetaType
</span><a href="#local-6989586621681174332"><span class="hs-identifier hs-var">cls_tys'</span></a></span><span> </span><span class="annot"><span class="annottext">ThetaType -&gt; ThetaType -&gt; ThetaType
forall a. [a] -&gt; [a] -&gt; [a]
</span><a href="../../base/src/GHC.Base.html#%2B%2B"><span class="hs-operator hs-var">++</span></a></span><span> </span><span class="hs-special">[</span><span class="annot"><span class="annottext">PredType
</span><a href="#local-6989586621681174334"><span class="hs-identifier hs-var">ty</span></a></span><span class="hs-special">]</span><span class="hs-special">)</span><span class="hs-special">)</span><span>
</span><span id="line-297"></span><span>           </span><span id="local-6989586621681174332"><span class="annot"><span class="annottext">cls_tys' :: ThetaType
</span><a href="#local-6989586621681174332"><span class="hs-identifier hs-var hs-var">cls_tys'</span></a></span></span><span> </span><span class="hs-glyph">|</span><span> </span><span class="annot"><span class="annottext">Bool
</span><a href="#local-6989586621681174392"><span class="hs-identifier hs-var">is_generic1</span></a></span><span> </span><span class="hs-glyph">=</span><span> </span><span class="hs-special">[</span><span class="hs-special">]</span><span>
</span><span id="line-298"></span><span>                      </span><span class="hs-comment">-- In the awkward Generic1 case, cls_tys' should be</span><span>
</span><span id="line-299"></span><span>                      </span><span class="hs-comment">-- empty, since we are applying the class Functor.</span><span>
</span><span id="line-300"></span><span>
</span><span id="line-301"></span><span>                    </span><span class="hs-glyph">|</span><span> </span><span class="annot"><span class="annottext">Bool
</span><a href="../../base/src/GHC.Base.html#otherwise"><span class="hs-identifier hs-var">otherwise</span></a></span><span>   </span><span class="hs-glyph">=</span><span> </span><span class="annot"><span class="annottext">ThetaType
</span><a href="#local-6989586621681174472"><span class="hs-identifier hs-var">cls_tys</span></a></span><span>
</span><span id="line-302"></span><span>
</span><span id="line-303"></span><span>           </span><span id="local-6989586621681174352"><span class="annot"><span class="annottext">deriv_origin :: CtOrigin
</span><a href="#local-6989586621681174352"><span class="hs-identifier hs-var hs-var">deriv_origin</span></a></span></span><span> </span><span class="hs-glyph">=</span><span> </span><span class="annot"><span class="annottext">Bool -&gt; CtOrigin
</span><a href="GHC.Tc.Deriv.Utils.html#mkDerivOrigin"><span class="hs-identifier hs-var">mkDerivOrigin</span></a></span><span> </span><span class="annot"><span class="annottext">Bool
</span><a href="#local-6989586621681174460"><span class="hs-identifier hs-var">wildcard</span></a></span><span>
</span><span id="line-304"></span><span>
</span><span id="line-305"></span><span>       </span><span class="hs-keyword">if</span><span>    </span><span class="hs-comment">-- Generic constraints are easy</span><span>
</span><span id="line-306"></span><span>          </span><span class="hs-glyph">|</span><span>  </span><span class="annot"><span class="annottext">Bool
</span><a href="#local-6989586621681174396"><span class="hs-identifier hs-var">is_generic</span></a></span><span>
</span><span id="line-307"></span><span>           </span><span class="hs-glyph">-&gt;</span><span> </span><span class="annot"><span class="annottext">([ThetaOrigin], [TcTyVar], ThetaType)
-&gt; DerivM ([ThetaOrigin], [TcTyVar], ThetaType)
forall (m :: * -&gt; *) a. Monad m =&gt; a -&gt; m a
</span><a href="../../base/src/GHC.Base.html#return"><span class="hs-identifier hs-var">return</span></a></span><span> </span><span class="hs-special">(</span><span class="hs-special">[</span><span class="hs-special">]</span><span class="hs-special">,</span><span> </span><span class="annot"><span class="annottext">[TcTyVar]
</span><a href="#local-6989586621681174463"><span class="hs-identifier hs-var">tvs</span></a></span><span class="hs-special">,</span><span> </span><span class="annot"><span class="annottext">ThetaType
</span><a href="#local-6989586621681174461"><span class="hs-identifier hs-var">inst_tys</span></a></span><span class="hs-special">)</span><span>
</span><span id="line-308"></span><span>
</span><span id="line-309"></span><span>             </span><span class="hs-comment">-- Generic1 needs Functor</span><span>
</span><span id="line-310"></span><span>             </span><span class="hs-comment">-- See Note [Getting base classes]</span><span>
</span><span id="line-311"></span><span>          </span><span class="hs-glyph">|</span><span>  </span><span class="annot"><span class="annottext">Bool
</span><a href="#local-6989586621681174392"><span class="hs-identifier hs-var">is_generic1</span></a></span><span>
</span><span id="line-312"></span><span>           </span><span class="hs-glyph">-&gt;</span><span> </span><span class="hs-identifier">ASSERT</span><span class="hs-special">(</span><span> </span><span class="hs-identifier">rep_tc_tvs</span><span> </span><span class="hs-special">`</span><span class="hs-identifier">lengthExceeds</span><span class="hs-special">`</span><span> </span><span class="hs-number">0</span><span> </span><span class="hs-special">)</span><span>
</span><span id="line-313"></span><span>              </span><span class="hs-comment">-- Generic1 has a single kind variable</span><span>
</span><span id="line-314"></span><span>              </span><span class="hs-identifier">ASSERT</span><span class="hs-special">(</span><span> </span><span class="hs-identifier">cls_tys</span><span> </span><span class="hs-special">`</span><span class="hs-identifier">lengthIs</span><span class="hs-special">`</span><span> </span><span class="hs-number">1</span><span> </span><span class="hs-special">)</span><span>
</span><span id="line-315"></span><span>              </span><span class="hs-keyword">do</span><span> </span><span class="hs-special">{</span><span> </span><span id="local-6989586621681174329"><span class="annot"><span class="annottext">Class
</span><a href="#local-6989586621681174329"><span class="hs-identifier hs-var">functorClass</span></a></span></span><span> </span><span class="hs-glyph">&lt;-</span><span> </span><span class="annot"><span class="annottext">IOEnv (Env TcGblEnv TcLclEnv) Class
-&gt; ReaderT DerivEnv (IOEnv (Env TcGblEnv TcLclEnv)) Class
forall (t :: (* -&gt; *) -&gt; * -&gt; *) (m :: * -&gt; *) a.
(MonadTrans t, Monad m) =&gt;
m a -&gt; t m a
</span><a href="../../transformers/src/Control.Monad.Trans.Class.html#lift"><span class="hs-identifier hs-var">lift</span></a></span><span> </span><span class="annot"><span class="annottext">(IOEnv (Env TcGblEnv TcLclEnv) Class
 -&gt; ReaderT DerivEnv (IOEnv (Env TcGblEnv TcLclEnv)) Class)
-&gt; IOEnv (Env TcGblEnv TcLclEnv) Class
-&gt; ReaderT DerivEnv (IOEnv (Env TcGblEnv TcLclEnv)) Class
forall a b. (a -&gt; b) -&gt; a -&gt; b
</span><a href="../../base/src/GHC.Base.html#%24"><span class="hs-operator hs-var">$</span></a></span><span> </span><span class="annot"><span class="annottext">Name -&gt; IOEnv (Env TcGblEnv TcLclEnv) Class
</span><a href="GHC.Tc.Utils.Env.html#tcLookupClass"><span class="hs-identifier hs-var">tcLookupClass</span></a></span><span> </span><span class="annot"><span class="annottext">Name
</span><a href="GHC.Builtin.Names.html#functorClassName"><span class="hs-identifier hs-var">functorClassName</span></a></span><span>
</span><span id="line-316"></span><span>                 </span><span class="hs-special">;</span><span> </span><span class="annot"><span class="annottext">([ThetaOrigin], [TcTyVar], ThetaType)
-&gt; DerivM ([ThetaOrigin], [TcTyVar], ThetaType)
forall (f :: * -&gt; *) a. Applicative f =&gt; a -&gt; f a
</span><a href="../../base/src/GHC.Base.html#pure"><span class="hs-identifier hs-var">pure</span></a></span><span> </span><span class="annot"><span class="annottext">(([ThetaOrigin], [TcTyVar], ThetaType)
 -&gt; DerivM ([ThetaOrigin], [TcTyVar], ThetaType))
-&gt; ([ThetaOrigin], [TcTyVar], ThetaType)
-&gt; DerivM ([ThetaOrigin], [TcTyVar], ThetaType)
forall a b. (a -&gt; b) -&gt; a -&gt; b
</span><a href="../../base/src/GHC.Base.html#%24"><span class="hs-operator hs-var">$</span></a></span><span> </span><span class="annot"><span class="annottext">(CtOrigin
 -&gt; TypeOrKind -&gt; PredType -&gt; [([PredOrigin], Maybe TCvSubst)])
-&gt; ([ThetaOrigin], [TcTyVar], ThetaType)
</span><a href="#local-6989586621681174449"><span class="hs-identifier hs-var">con_arg_constraints</span></a></span><span>
</span><span id="line-317"></span><span>                        </span><span class="annot"><span class="annottext">((CtOrigin
  -&gt; TypeOrKind -&gt; PredType -&gt; [([PredOrigin], Maybe TCvSubst)])
 -&gt; ([ThetaOrigin], [TcTyVar], ThetaType))
-&gt; (CtOrigin
    -&gt; TypeOrKind -&gt; PredType -&gt; [([PredOrigin], Maybe TCvSubst)])
-&gt; ([ThetaOrigin], [TcTyVar], ThetaType)
forall a b. (a -&gt; b) -&gt; a -&gt; b
</span><a href="../../base/src/GHC.Base.html#%24"><span class="hs-operator hs-var">$</span></a></span><span> </span><span class="annot"><span class="annottext">Class
-&gt; CtOrigin
-&gt; TypeOrKind
-&gt; PredType
-&gt; [([PredOrigin], Maybe TCvSubst)]
</span><a href="#local-6989586621681174384"><span class="hs-identifier hs-var">get_gen1_constraints</span></a></span><span> </span><span class="annot"><span class="annottext">Class
</span><a href="#local-6989586621681174329"><span class="hs-identifier hs-var">functorClass</span></a></span><span> </span><span class="hs-special">}</span><span>
</span><span id="line-318"></span><span>
</span><span id="line-319"></span><span>             </span><span class="hs-comment">-- The others are a bit more complicated</span><span>
</span><span id="line-320"></span><span>          </span><span class="hs-glyph">|</span><span>  </span><span class="annot"><span class="annottext">Bool
</span><a href="../../base/src/GHC.Base.html#otherwise"><span class="hs-identifier hs-var">otherwise</span></a></span><span>
</span><span id="line-321"></span><span>           </span><span class="hs-glyph">-&gt;</span><span> </span><span class="hs-comment">-- See the comment with all_rep_tc_args for an explanation of</span><span>
</span><span id="line-322"></span><span>              </span><span class="hs-comment">-- this assertion</span><span>
</span><span id="line-323"></span><span>              </span><span class="hs-identifier">ASSERT2</span><span class="hs-special">(</span><span> </span><span class="hs-identifier">equalLength</span><span> </span><span class="hs-identifier">rep_tc_tvs</span><span> </span><span class="hs-identifier">all_rep_tc_args</span><span>
</span><span id="line-324"></span><span>                     </span><span class="hs-special">,</span><span> </span><span class="hs-identifier">ppr</span><span> </span><span class="hs-identifier">main_cls</span><span> </span><span class="hs-operator">&lt;+&gt;</span><span> </span><span class="hs-identifier">ppr</span><span> </span><span class="hs-identifier">rep_tc</span><span>
</span><span id="line-325"></span><span>                       </span><span class="hs-operator">$$</span><span> </span><span class="hs-identifier">ppr</span><span> </span><span class="hs-identifier">rep_tc_tvs</span><span> </span><span class="hs-operator">$$</span><span> </span><span class="hs-identifier">ppr</span><span> </span><span class="hs-identifier">all_rep_tc_args</span><span> </span><span class="hs-special">)</span><span>
</span><span id="line-326"></span><span>                </span><span class="hs-keyword">do</span><span> </span><span class="hs-special">{</span><span> </span><span class="hs-keyword">let</span><span> </span><span class="hs-special">(</span><span id="local-6989586621681174325"><span class="annot"><span class="annottext">[ThetaOrigin]
</span><a href="#local-6989586621681174325"><span class="hs-identifier hs-var">arg_constraints</span></a></span></span><span class="hs-special">,</span><span> </span><span id="local-6989586621681174324"><span class="annot"><span class="annottext">[TcTyVar]
</span><a href="#local-6989586621681174324"><span class="hs-identifier hs-var">tvs'</span></a></span></span><span class="hs-special">,</span><span> </span><span id="local-6989586621681174323"><span class="annot"><span class="annottext">ThetaType
</span><a href="#local-6989586621681174323"><span class="hs-identifier hs-var">inst_tys'</span></a></span></span><span class="hs-special">)</span><span>
</span><span id="line-327"></span><span>                           </span><span class="hs-glyph">=</span><span> </span><span class="annot"><span class="annottext">(CtOrigin
 -&gt; TypeOrKind -&gt; PredType -&gt; [([PredOrigin], Maybe TCvSubst)])
-&gt; ([ThetaOrigin], [TcTyVar], ThetaType)
</span><a href="#local-6989586621681174449"><span class="hs-identifier hs-var">con_arg_constraints</span></a></span><span> </span><span class="annot"><span class="annottext">CtOrigin
-&gt; TypeOrKind -&gt; PredType -&gt; [([PredOrigin], Maybe TCvSubst)]
</span><a href="#local-6989586621681174376"><span class="hs-identifier hs-var">get_std_constrained_tys</span></a></span><span>
</span><span id="line-328"></span><span>                   </span><span class="hs-special">;</span><span> </span><span class="annot"><span class="annottext">IOEnv (Env TcGblEnv TcLclEnv) ()
-&gt; ReaderT DerivEnv (IOEnv (Env TcGblEnv TcLclEnv)) ()
forall (t :: (* -&gt; *) -&gt; * -&gt; *) (m :: * -&gt; *) a.
(MonadTrans t, Monad m) =&gt;
m a -&gt; t m a
</span><a href="../../transformers/src/Control.Monad.Trans.Class.html#lift"><span class="hs-identifier hs-var">lift</span></a></span><span> </span><span class="annot"><span class="annottext">(IOEnv (Env TcGblEnv TcLclEnv) ()
 -&gt; ReaderT DerivEnv (IOEnv (Env TcGblEnv TcLclEnv)) ())
-&gt; IOEnv (Env TcGblEnv TcLclEnv) ()
-&gt; ReaderT DerivEnv (IOEnv (Env TcGblEnv TcLclEnv)) ()
forall a b. (a -&gt; b) -&gt; a -&gt; b
</span><a href="../../base/src/GHC.Base.html#%24"><span class="hs-operator hs-var">$</span></a></span><span> </span><span class="annot"><span class="annottext">String -&gt; SDoc -&gt; IOEnv (Env TcGblEnv TcLclEnv) ()
</span><a href="GHC.Tc.Utils.Monad.html#traceTc"><span class="hs-identifier hs-var">traceTc</span></a></span><span> </span><span class="annot"><span class="annottext">String
</span><span class="hs-string">&quot;inferConstraintsStock&quot;</span></span><span> </span><span class="annot"><span class="annottext">(SDoc -&gt; IOEnv (Env TcGblEnv TcLclEnv) ())
-&gt; SDoc -&gt; IOEnv (Env TcGblEnv TcLclEnv) ()
forall a b. (a -&gt; b) -&gt; a -&gt; b
</span><a href="../../base/src/GHC.Base.html#%24"><span class="hs-operator hs-var">$</span></a></span><span> </span><span class="annot"><span class="annottext">[SDoc] -&gt; SDoc
</span><a href="GHC.Utils.Outputable.html#vcat"><span class="hs-identifier hs-var">vcat</span></a></span><span>
</span><span id="line-329"></span><span>                          </span><span class="hs-special">[</span><span> </span><span class="annot"><span class="annottext">Class -&gt; SDoc
forall a. Outputable a =&gt; a -&gt; SDoc
</span><a href="GHC.Utils.Outputable.html#ppr"><span class="hs-identifier hs-var">ppr</span></a></span><span> </span><span class="annot"><span class="annottext">Class
</span><a href="#local-6989586621681174462"><span class="hs-identifier hs-var">main_cls</span></a></span><span> </span><span class="annot"><span class="annottext">SDoc -&gt; SDoc -&gt; SDoc
</span><a href="GHC.Utils.Outputable.html#%3C%2B%3E"><span class="hs-operator hs-var">&lt;+&gt;</span></a></span><span> </span><span class="annot"><span class="annottext">ThetaType -&gt; SDoc
forall a. Outputable a =&gt; a -&gt; SDoc
</span><a href="GHC.Utils.Outputable.html#ppr"><span class="hs-identifier hs-var">ppr</span></a></span><span> </span><span class="annot"><span class="annottext">ThetaType
</span><a href="#local-6989586621681174323"><span class="hs-identifier hs-var">inst_tys'</span></a></span><span>
</span><span id="line-330"></span><span>                          </span><span class="hs-special">,</span><span> </span><span class="annot"><span class="annottext">[ThetaOrigin] -&gt; SDoc
forall a. Outputable a =&gt; a -&gt; SDoc
</span><a href="GHC.Utils.Outputable.html#ppr"><span class="hs-identifier hs-var">ppr</span></a></span><span> </span><span class="annot"><span class="annottext">[ThetaOrigin]
</span><a href="#local-6989586621681174325"><span class="hs-identifier hs-var">arg_constraints</span></a></span><span>
</span><span id="line-331"></span><span>                          </span><span class="hs-special">]</span><span>
</span><span id="line-332"></span><span>                   </span><span class="hs-special">;</span><span> </span><span class="annot"><span class="annottext">([ThetaOrigin], [TcTyVar], ThetaType)
-&gt; DerivM ([ThetaOrigin], [TcTyVar], ThetaType)
forall (m :: * -&gt; *) a. Monad m =&gt; a -&gt; m a
</span><a href="../../base/src/GHC.Base.html#return"><span class="hs-identifier hs-var">return</span></a></span><span> </span><span class="hs-special">(</span><span> </span><span class="annot"><span class="annottext">[ThetaOrigin]
</span><a href="#local-6989586621681174353"><span class="hs-identifier hs-var">stupid_constraints</span></a></span><span> </span><span class="annot"><span class="annottext">[ThetaOrigin] -&gt; [ThetaOrigin] -&gt; [ThetaOrigin]
forall a. [a] -&gt; [a] -&gt; [a]
</span><a href="../../base/src/GHC.Base.html#%2B%2B"><span class="hs-operator hs-var">++</span></a></span><span> </span><span class="annot"><span class="annottext">[ThetaOrigin]
</span><a href="#local-6989586621681174348"><span class="hs-identifier hs-var">extra_constraints</span></a></span><span>
</span><span id="line-333"></span><span>                                                 </span><span class="annot"><span class="annottext">[ThetaOrigin] -&gt; [ThetaOrigin] -&gt; [ThetaOrigin]
forall a. [a] -&gt; [a] -&gt; [a]
</span><a href="../../base/src/GHC.Base.html#%2B%2B"><span class="hs-operator hs-var">++</span></a></span><span> </span><span class="annot"><span class="annottext">[ThetaOrigin]
</span><a href="#local-6989586621681174325"><span class="hs-identifier hs-var">arg_constraints</span></a></span><span>
</span><span id="line-334"></span><span>                            </span><span class="hs-special">,</span><span> </span><span class="annot"><span class="annottext">[TcTyVar]
</span><a href="#local-6989586621681174324"><span class="hs-identifier hs-var">tvs'</span></a></span><span class="hs-special">,</span><span> </span><span class="annot"><span class="annottext">ThetaType
</span><a href="#local-6989586621681174323"><span class="hs-identifier hs-var">inst_tys'</span></a></span><span class="hs-special">)</span><span> </span><span class="hs-special">}</span><span>
</span><span id="line-335"></span><span>
</span><span id="line-336"></span><span class="hs-comment">-- | Like 'inferConstraints', but used only in the case of @DeriveAnyClass@,</span><span>
</span><span id="line-337"></span><span class="hs-comment">-- which gathers its constraints based on the type signatures of the class's</span><span>
</span><span id="line-338"></span><span class="hs-comment">-- methods instead of the types of the data constructor's field.</span><span>
</span><span id="line-339"></span><span class="hs-comment">--</span><span>
</span><span id="line-340"></span><span class="hs-comment">-- See Note [Gathering and simplifying constraints for DeriveAnyClass]</span><span>
</span><span id="line-341"></span><span class="hs-comment">-- for an explanation of how these constraints are used to determine the</span><span>
</span><span id="line-342"></span><span class="hs-comment">-- derived instance context.</span><span>
</span><span id="line-343"></span><span class="annot"><a href="GHC.Tc.Deriv.Infer.html#inferConstraintsAnyclass"><span class="hs-identifier hs-type">inferConstraintsAnyclass</span></a></span><span> </span><span class="hs-glyph">::</span><span> </span><span class="annot"><a href="GHC.Tc.Deriv.Utils.html#DerivM"><span class="hs-identifier hs-type">DerivM</span></a></span><span> </span><span class="hs-special">[</span><span class="annot"><a href="GHC.Tc.Deriv.Utils.html#ThetaOrigin"><span class="hs-identifier hs-type">ThetaOrigin</span></a></span><span class="hs-special">]</span><span>
</span><span id="line-344"></span><span id="inferConstraintsAnyclass"><span class="annot"><span class="annottext">inferConstraintsAnyclass :: DerivM [ThetaOrigin]
</span><a href="GHC.Tc.Deriv.Infer.html#inferConstraintsAnyclass"><span class="hs-identifier hs-var hs-var">inferConstraintsAnyclass</span></a></span></span><span>
</span><span id="line-345"></span><span>  </span><span class="hs-glyph">=</span><span> </span><span class="hs-keyword">do</span><span> </span><span class="hs-special">{</span><span> </span><span class="annot"><a href="GHC.Tc.Deriv.Utils.html#DerivEnv"><span class="hs-identifier hs-type">DerivEnv</span></a></span><span> </span><span class="hs-special">{</span><span> </span><span class="annot"><span class="annottext">denv_cls :: DerivEnv -&gt; Class
</span><a href="GHC.Tc.Deriv.Utils.html#denv_cls"><span class="hs-identifier hs-var">denv_cls</span></a></span><span>      </span><span class="hs-glyph">=</span><span> </span><span id="local-6989586621681174322"><span class="annot"><span class="annottext">Class
</span><a href="#local-6989586621681174322"><span class="hs-identifier hs-var">cls</span></a></span></span><span>
</span><span id="line-346"></span><span>                  </span><span class="hs-special">,</span><span> </span><span class="annot"><span class="annottext">denv_inst_tys :: DerivEnv -&gt; ThetaType
</span><a href="GHC.Tc.Deriv.Utils.html#denv_inst_tys"><span class="hs-identifier hs-var">denv_inst_tys</span></a></span><span> </span><span class="hs-glyph">=</span><span> </span><span id="local-6989586621681174321"><span class="annot"><span class="annottext">ThetaType
</span><a href="#local-6989586621681174321"><span class="hs-identifier hs-var">inst_tys</span></a></span></span><span> </span><span class="hs-special">}</span><span> </span><span class="hs-glyph">&lt;-</span><span> </span><span class="annot"><span class="annottext">ReaderT DerivEnv (IOEnv (Env TcGblEnv TcLclEnv)) DerivEnv
forall (m :: * -&gt; *) r. Monad m =&gt; ReaderT r m r
</span><a href="../../transformers/src/Control.Monad.Trans.Reader.html#ask"><span class="hs-identifier hs-var">ask</span></a></span><span>
</span><span id="line-347"></span><span>       </span><span class="hs-special">;</span><span> </span><span id="local-6989586621681174320"><span class="annot"><span class="annottext">Bool
</span><a href="#local-6989586621681174320"><span class="hs-identifier hs-var">wildcard</span></a></span></span><span> </span><span class="hs-glyph">&lt;-</span><span> </span><span class="annot"><span class="annottext">DerivM Bool
</span><a href="GHC.Tc.Deriv.Utils.html#isStandaloneWildcardDeriv"><span class="hs-identifier hs-var">isStandaloneWildcardDeriv</span></a></span><span>
</span><span id="line-348"></span><span>
</span><span id="line-349"></span><span>       </span><span class="hs-special">;</span><span> </span><span class="hs-keyword">let</span><span> </span><span id="local-6989586621681174319"><span class="annot"><span class="annottext">gen_dms :: [(TcTyVar, PredType)]
</span><a href="#local-6989586621681174319"><span class="hs-identifier hs-var hs-var">gen_dms</span></a></span></span><span> </span><span class="hs-glyph">=</span><span> </span><span class="hs-special">[</span><span> </span><span class="hs-special">(</span><span class="annot"><span class="annottext">TcTyVar
</span><a href="#local-6989586621681174318"><span class="hs-identifier hs-var">sel_id</span></a></span><span class="hs-special">,</span><span> </span><span class="annot"><span class="annottext">PredType
</span><a href="#local-6989586621681174317"><span class="hs-identifier hs-var">dm_ty</span></a></span><span class="hs-special">)</span><span>
</span><span id="line-350"></span><span>                       </span><span class="hs-glyph">|</span><span> </span><span class="hs-special">(</span><span id="local-6989586621681174318"><span class="annot"><span class="annottext">TcTyVar
</span><a href="#local-6989586621681174318"><span class="hs-identifier hs-var">sel_id</span></a></span></span><span class="hs-special">,</span><span> </span><span class="annot"><a href="../../base/src/GHC.Maybe.html#Just"><span class="hs-identifier hs-type">Just</span></a></span><span> </span><span class="hs-special">(</span><span class="annot"><span class="annottext">Name
</span><span class="hs-identifier">_</span></span><span class="hs-special">,</span><span> </span><span class="annot"><a href="GHC.Types.Basic.html#GenericDM"><span class="hs-identifier hs-type">GenericDM</span></a></span><span> </span><span id="local-6989586621681174317"><span class="annot"><span class="annottext">PredType
</span><a href="#local-6989586621681174317"><span class="hs-identifier hs-var">dm_ty</span></a></span></span><span class="hs-special">)</span><span class="hs-special">)</span><span> </span><span class="hs-glyph">&lt;-</span><span> </span><span class="annot"><span class="annottext">Class -&gt; [(TcTyVar, DefMethInfo)]
</span><a href="GHC.Core.Class.html#classOpItems"><span class="hs-identifier hs-var">classOpItems</span></a></span><span> </span><span class="annot"><span class="annottext">Class
</span><a href="#local-6989586621681174322"><span class="hs-identifier hs-var">cls</span></a></span><span> </span><span class="hs-special">]</span><span>
</span><span id="line-351"></span><span>
</span><span id="line-352"></span><span>             </span><span id="local-6989586621681174314"><span class="annot"><span class="annottext">cls_tvs :: [TcTyVar]
</span><a href="#local-6989586621681174314"><span class="hs-identifier hs-var hs-var">cls_tvs</span></a></span></span><span> </span><span class="hs-glyph">=</span><span> </span><span class="annot"><span class="annottext">Class -&gt; [TcTyVar]
</span><a href="GHC.Core.Class.html#classTyVars"><span class="hs-identifier hs-var hs-var">classTyVars</span></a></span><span> </span><span class="annot"><span class="annottext">Class
</span><a href="#local-6989586621681174322"><span class="hs-identifier hs-var">cls</span></a></span><span>
</span><span id="line-353"></span><span>
</span><span id="line-354"></span><span>             </span><span class="annot"><a href="#local-6989586621681174313"><span class="hs-identifier hs-type">do_one_meth</span></a></span><span> </span><span class="hs-glyph">::</span><span> </span><span class="hs-special">(</span><span class="annot"><a href="GHC.Types.Var.html#Id"><span class="hs-identifier hs-type">Id</span></a></span><span class="hs-special">,</span><span> </span><span class="annot"><a href="GHC.Core.TyCo.Rep.html#Type"><span class="hs-identifier hs-type">Type</span></a></span><span class="hs-special">)</span><span> </span><span class="hs-glyph">-&gt;</span><span> </span><span class="annot"><a href="GHC.Tc.Types.html#TcM"><span class="hs-identifier hs-type">TcM</span></a></span><span> </span><span class="annot"><a href="GHC.Tc.Deriv.Utils.html#ThetaOrigin"><span class="hs-identifier hs-type">ThetaOrigin</span></a></span><span>
</span><span id="line-355"></span><span>               </span><span class="hs-comment">-- (Id,Type) are the selector Id and the generic default method type</span><span>
</span><span id="line-356"></span><span>               </span><span class="hs-comment">-- NB: the latter is /not/ quantified over the class variables</span><span>
</span><span id="line-357"></span><span>               </span><span class="hs-comment">-- See Note [Gathering and simplifying constraints for DeriveAnyClass]</span><span>
</span><span id="line-358"></span><span>             </span><span id="local-6989586621681174313"><span class="annot"><span class="annottext">do_one_meth :: (TcTyVar, PredType) -&gt; TcM ThetaOrigin
</span><a href="#local-6989586621681174313"><span class="hs-identifier hs-var hs-var">do_one_meth</span></a></span></span><span> </span><span class="hs-special">(</span><span id="local-6989586621681174311"><span class="annot"><span class="annottext">TcTyVar
</span><a href="#local-6989586621681174311"><span class="hs-identifier hs-var">sel_id</span></a></span></span><span class="hs-special">,</span><span> </span><span id="local-6989586621681174310"><span class="annot"><span class="annottext">PredType
</span><a href="#local-6989586621681174310"><span class="hs-identifier hs-var">gen_dm_ty</span></a></span></span><span class="hs-special">)</span><span>
</span><span id="line-359"></span><span>               </span><span class="hs-glyph">=</span><span> </span><span class="hs-keyword">do</span><span> </span><span class="hs-special">{</span><span> </span><span class="hs-keyword">let</span><span> </span><span class="hs-special">(</span><span id="local-6989586621681174309"><span class="annot"><span class="annottext">[TcTyVar]
</span><a href="#local-6989586621681174309"><span class="hs-identifier hs-var">sel_tvs</span></a></span></span><span class="hs-special">,</span><span> </span><span id="local-6989586621681174308"><span class="annot"><span class="annottext">PredType
</span><a href="#local-6989586621681174308"><span class="hs-identifier hs-var">_cls_pred</span></a></span></span><span class="hs-special">,</span><span> </span><span id="local-6989586621681174307"><span class="annot"><span class="annottext">PredType
</span><a href="#local-6989586621681174307"><span class="hs-identifier hs-var">meth_ty</span></a></span></span><span class="hs-special">)</span><span>
</span><span id="line-360"></span><span>                                   </span><span class="hs-glyph">=</span><span> </span><span class="annot"><span class="annottext">PredType -&gt; ([TcTyVar], PredType, PredType)
</span><a href="GHC.Tc.Utils.TcType.html#tcSplitMethodTy"><span class="hs-identifier hs-var">tcSplitMethodTy</span></a></span><span> </span><span class="hs-special">(</span><span class="annot"><span class="annottext">TcTyVar -&gt; PredType
</span><a href="GHC.Types.Var.html#varType"><span class="hs-identifier hs-var hs-var">varType</span></a></span><span> </span><span class="annot"><span class="annottext">TcTyVar
</span><a href="#local-6989586621681174311"><span class="hs-identifier hs-var">sel_id</span></a></span><span class="hs-special">)</span><span>
</span><span id="line-361"></span><span>                          </span><span id="local-6989586621681174302"><span class="annot"><span class="annottext">meth_ty' :: PredType
</span><a href="#local-6989586621681174302"><span class="hs-identifier hs-var hs-var">meth_ty'</span></a></span></span><span> </span><span class="hs-glyph">=</span><span> </span><span class="annot"><span class="annottext">HasCallStack =&gt; [TcTyVar] -&gt; ThetaType -&gt; PredType -&gt; PredType
[TcTyVar] -&gt; ThetaType -&gt; PredType -&gt; PredType
</span><a href="GHC.Core.TyCo.Subst.html#substTyWith"><span class="hs-identifier hs-var">substTyWith</span></a></span><span> </span><span class="annot"><span class="annottext">[TcTyVar]
</span><a href="#local-6989586621681174309"><span class="hs-identifier hs-var">sel_tvs</span></a></span><span> </span><span class="annot"><span class="annottext">ThetaType
</span><a href="#local-6989586621681174321"><span class="hs-identifier hs-var">inst_tys</span></a></span><span> </span><span class="annot"><span class="annottext">PredType
</span><a href="#local-6989586621681174307"><span class="hs-identifier hs-var">meth_ty</span></a></span><span>
</span><span id="line-362"></span><span>                          </span><span class="hs-special">(</span><span id="local-6989586621681174300"><span class="annot"><span class="annottext">[TcTyVar]
</span><a href="#local-6989586621681174300"><span class="hs-identifier hs-var">meth_tvs</span></a></span></span><span class="hs-special">,</span><span> </span><span id="local-6989586621681174299"><span class="annot"><span class="annottext">ThetaType
</span><a href="#local-6989586621681174299"><span class="hs-identifier hs-var">meth_theta</span></a></span></span><span class="hs-special">,</span><span> </span><span id="local-6989586621681174298"><span class="annot"><span class="annottext">PredType
</span><a href="#local-6989586621681174298"><span class="hs-identifier hs-var">meth_tau</span></a></span></span><span class="hs-special">)</span><span>
</span><span id="line-363"></span><span>                                   </span><span class="hs-glyph">=</span><span> </span><span class="annot"><span class="annottext">PredType -&gt; ([TcTyVar], ThetaType, PredType)
</span><a href="GHC.Tc.Utils.TcType.html#tcSplitNestedSigmaTys"><span class="hs-identifier hs-var">tcSplitNestedSigmaTys</span></a></span><span> </span><span class="annot"><span class="annottext">PredType
</span><a href="#local-6989586621681174302"><span class="hs-identifier hs-var">meth_ty'</span></a></span><span>
</span><span id="line-364"></span><span>
</span><span id="line-365"></span><span>                          </span><span id="local-6989586621681174294"><span class="annot"><span class="annottext">gen_dm_ty' :: PredType
</span><a href="#local-6989586621681174294"><span class="hs-identifier hs-var hs-var">gen_dm_ty'</span></a></span></span><span> </span><span class="hs-glyph">=</span><span> </span><span class="annot"><span class="annottext">HasCallStack =&gt; [TcTyVar] -&gt; ThetaType -&gt; PredType -&gt; PredType
[TcTyVar] -&gt; ThetaType -&gt; PredType -&gt; PredType
</span><a href="GHC.Core.TyCo.Subst.html#substTyWith"><span class="hs-identifier hs-var">substTyWith</span></a></span><span> </span><span class="annot"><span class="annottext">[TcTyVar]
</span><a href="#local-6989586621681174314"><span class="hs-identifier hs-var">cls_tvs</span></a></span><span> </span><span class="annot"><span class="annottext">ThetaType
</span><a href="#local-6989586621681174321"><span class="hs-identifier hs-var">inst_tys</span></a></span><span> </span><span class="annot"><span class="annottext">PredType
</span><a href="#local-6989586621681174310"><span class="hs-identifier hs-var">gen_dm_ty</span></a></span><span>
</span><span id="line-366"></span><span>                          </span><span class="hs-special">(</span><span id="local-6989586621681174293"><span class="annot"><span class="annottext">[TcTyVar]
</span><a href="#local-6989586621681174293"><span class="hs-identifier hs-var">dm_tvs</span></a></span></span><span class="hs-special">,</span><span> </span><span id="local-6989586621681174292"><span class="annot"><span class="annottext">ThetaType
</span><a href="#local-6989586621681174292"><span class="hs-identifier hs-var">dm_theta</span></a></span></span><span class="hs-special">,</span><span> </span><span id="local-6989586621681174291"><span class="annot"><span class="annottext">PredType
</span><a href="#local-6989586621681174291"><span class="hs-identifier hs-var">dm_tau</span></a></span></span><span class="hs-special">)</span><span>
</span><span id="line-367"></span><span>                                     </span><span class="hs-glyph">=</span><span> </span><span class="annot"><span class="annottext">PredType -&gt; ([TcTyVar], ThetaType, PredType)
</span><a href="GHC.Tc.Utils.TcType.html#tcSplitNestedSigmaTys"><span class="hs-identifier hs-var">tcSplitNestedSigmaTys</span></a></span><span> </span><span class="annot"><span class="annottext">PredType
</span><a href="#local-6989586621681174294"><span class="hs-identifier hs-var">gen_dm_ty'</span></a></span><span>
</span><span id="line-368"></span><span>                          </span><span id="local-6989586621681174290"><span class="annot"><span class="annottext">tau_eq :: PredType
</span><a href="#local-6989586621681174290"><span class="hs-identifier hs-var hs-var">tau_eq</span></a></span></span><span>     </span><span class="hs-glyph">=</span><span> </span><span class="annot"><span class="annottext">PredType -&gt; PredType -&gt; PredType
</span><a href="GHC.Core.Coercion.html#mkPrimEqPred"><span class="hs-identifier hs-var">mkPrimEqPred</span></a></span><span> </span><span class="annot"><span class="annottext">PredType
</span><a href="#local-6989586621681174298"><span class="hs-identifier hs-var">meth_tau</span></a></span><span> </span><span class="annot"><span class="annottext">PredType
</span><a href="#local-6989586621681174291"><span class="hs-identifier hs-var">dm_tau</span></a></span><span>
</span><span id="line-369"></span><span>                    </span><span class="hs-special">;</span><span> </span><span class="annot"><span class="annottext">ThetaOrigin -&gt; TcM ThetaOrigin
forall (m :: * -&gt; *) a. Monad m =&gt; a -&gt; m a
</span><a href="../../base/src/GHC.Base.html#return"><span class="hs-identifier hs-var">return</span></a></span><span> </span><span class="hs-special">(</span><span class="annot"><span class="annottext">CtOrigin
-&gt; TypeOrKind
-&gt; [TcTyVar]
-&gt; [TcTyVar]
-&gt; ThetaType
-&gt; ThetaType
-&gt; ThetaOrigin
</span><a href="GHC.Tc.Deriv.Utils.html#mkThetaOrigin"><span class="hs-identifier hs-var">mkThetaOrigin</span></a></span><span> </span><span class="hs-special">(</span><span class="annot"><span class="annottext">Bool -&gt; CtOrigin
</span><a href="GHC.Tc.Deriv.Utils.html#mkDerivOrigin"><span class="hs-identifier hs-var">mkDerivOrigin</span></a></span><span> </span><span class="annot"><span class="annottext">Bool
</span><a href="#local-6989586621681174320"><span class="hs-identifier hs-var">wildcard</span></a></span><span class="hs-special">)</span><span> </span><span class="annot"><span class="annottext">TypeOrKind
</span><a href="GHC.Types.Basic.html#TypeLevel"><span class="hs-identifier hs-var">TypeLevel</span></a></span><span>
</span><span id="line-370"></span><span>                                </span><span class="annot"><span class="annottext">[TcTyVar]
</span><a href="#local-6989586621681174300"><span class="hs-identifier hs-var">meth_tvs</span></a></span><span> </span><span class="annot"><span class="annottext">[TcTyVar]
</span><a href="#local-6989586621681174293"><span class="hs-identifier hs-var">dm_tvs</span></a></span><span> </span><span class="annot"><span class="annottext">ThetaType
</span><a href="#local-6989586621681174299"><span class="hs-identifier hs-var">meth_theta</span></a></span><span> </span><span class="hs-special">(</span><span class="annot"><span class="annottext">PredType
</span><a href="#local-6989586621681174290"><span class="hs-identifier hs-var">tau_eq</span></a></span><span class="annot"><span class="annottext">PredType -&gt; ThetaType -&gt; ThetaType
forall a. a -&gt; [a] -&gt; [a]
</span><span class="hs-glyph hs-var">:</span></span><span class="annot"><span class="annottext">ThetaType
</span><a href="#local-6989586621681174292"><span class="hs-identifier hs-var">dm_theta</span></a></span><span class="hs-special">)</span><span class="hs-special">)</span><span> </span><span class="hs-special">}</span><span>
</span><span id="line-371"></span><span>
</span><span id="line-372"></span><span>       </span><span class="hs-special">;</span><span> </span><span id="local-6989586621681174289"><span class="annot"><span class="annottext">[ThetaOrigin]
</span><a href="#local-6989586621681174289"><span class="hs-identifier hs-var">theta_origins</span></a></span></span><span> </span><span class="hs-glyph">&lt;-</span><span> </span><span class="annot"><span class="annottext">IOEnv (Env TcGblEnv TcLclEnv) [ThetaOrigin] -&gt; DerivM [ThetaOrigin]
forall (t :: (* -&gt; *) -&gt; * -&gt; *) (m :: * -&gt; *) a.
(MonadTrans t, Monad m) =&gt;
m a -&gt; t m a
</span><a href="../../transformers/src/Control.Monad.Trans.Class.html#lift"><span class="hs-identifier hs-var">lift</span></a></span><span> </span><span class="annot"><span class="annottext">(IOEnv (Env TcGblEnv TcLclEnv) [ThetaOrigin]
 -&gt; DerivM [ThetaOrigin])
-&gt; IOEnv (Env TcGblEnv TcLclEnv) [ThetaOrigin]
-&gt; DerivM [ThetaOrigin]
forall a b. (a -&gt; b) -&gt; a -&gt; b
</span><a href="../../base/src/GHC.Base.html#%24"><span class="hs-operator hs-var">$</span></a></span><span> </span><span class="annot"><span class="annottext">((TcTyVar, PredType) -&gt; TcM ThetaOrigin)
-&gt; [(TcTyVar, PredType)]
-&gt; IOEnv (Env TcGblEnv TcLclEnv) [ThetaOrigin]
forall (t :: * -&gt; *) (m :: * -&gt; *) a b.
(Traversable t, Monad m) =&gt;
(a -&gt; m b) -&gt; t a -&gt; m (t b)
</span><a href="../../base/src/Data.Traversable.html#mapM"><span class="hs-identifier hs-var">mapM</span></a></span><span> </span><span class="annot"><span class="annottext">(TcTyVar, PredType) -&gt; TcM ThetaOrigin
</span><a href="#local-6989586621681174313"><span class="hs-identifier hs-var">do_one_meth</span></a></span><span> </span><span class="annot"><span class="annottext">[(TcTyVar, PredType)]
</span><a href="#local-6989586621681174319"><span class="hs-identifier hs-var">gen_dms</span></a></span><span>
</span><span id="line-373"></span><span>       </span><span class="hs-special">;</span><span> </span><span class="annot"><span class="annottext">[ThetaOrigin] -&gt; DerivM [ThetaOrigin]
forall (m :: * -&gt; *) a. Monad m =&gt; a -&gt; m a
</span><a href="../../base/src/GHC.Base.html#return"><span class="hs-identifier hs-var">return</span></a></span><span> </span><span class="annot"><span class="annottext">[ThetaOrigin]
</span><a href="#local-6989586621681174289"><span class="hs-identifier hs-var">theta_origins</span></a></span><span> </span><span class="hs-special">}</span><span>
</span><span id="line-374"></span><span>
</span><span id="line-375"></span><span class="hs-comment">-- Like 'inferConstraints', but used only for @GeneralizedNewtypeDeriving@ and</span><span>
</span><span id="line-376"></span><span class="hs-comment">-- @DerivingVia@. Since both strategies generate code involving 'coerce', the</span><span>
</span><span id="line-377"></span><span class="hs-comment">-- inferred constraints set up the scaffolding needed to typecheck those uses</span><span>
</span><span id="line-378"></span><span class="hs-comment">-- of 'coerce'. In this example:</span><span>
</span><span id="line-379"></span><span class="hs-comment">--</span><span>
</span><span id="line-380"></span><span class="hs-comment">-- &gt; newtype Age = MkAge Int deriving newtype Num</span><span>
</span><span id="line-381"></span><span class="hs-comment">--</span><span>
</span><span id="line-382"></span><span class="hs-comment">-- We would infer the following constraints ('ThetaOrigin's):</span><span>
</span><span id="line-383"></span><span class="hs-comment">--</span><span>
</span><span id="line-384"></span><span class="hs-comment">-- &gt; (Num Int, Coercible Age Int)</span><span>
</span><span id="line-385"></span><span class="annot"><a href="GHC.Tc.Deriv.Infer.html#inferConstraintsCoerceBased"><span class="hs-identifier hs-type">inferConstraintsCoerceBased</span></a></span><span> </span><span class="hs-glyph">::</span><span> </span><span class="hs-special">[</span><span class="annot"><a href="GHC.Core.TyCo.Rep.html#Type"><span class="hs-identifier hs-type">Type</span></a></span><span class="hs-special">]</span><span> </span><span class="hs-glyph">-&gt;</span><span> </span><span class="annot"><a href="GHC.Core.TyCo.Rep.html#Type"><span class="hs-identifier hs-type">Type</span></a></span><span>
</span><span id="line-386"></span><span>                            </span><span class="hs-glyph">-&gt;</span><span> </span><span class="annot"><a href="GHC.Tc.Deriv.Utils.html#DerivM"><span class="hs-identifier hs-type">DerivM</span></a></span><span> </span><span class="hs-special">[</span><span class="annot"><a href="GHC.Tc.Deriv.Utils.html#ThetaOrigin"><span class="hs-identifier hs-type">ThetaOrigin</span></a></span><span class="hs-special">]</span><span>
</span><span id="line-387"></span><span id="inferConstraintsCoerceBased"><span class="annot"><span class="annottext">inferConstraintsCoerceBased :: ThetaType -&gt; PredType -&gt; DerivM [ThetaOrigin]
</span><a href="GHC.Tc.Deriv.Infer.html#inferConstraintsCoerceBased"><span class="hs-identifier hs-var hs-var">inferConstraintsCoerceBased</span></a></span></span><span> </span><span id="local-6989586621681174287"><span class="annot"><span class="annottext">ThetaType
</span><a href="#local-6989586621681174287"><span class="hs-identifier hs-var">cls_tys</span></a></span></span><span> </span><span id="local-6989586621681174286"><span class="annot"><span class="annottext">PredType
</span><a href="#local-6989586621681174286"><span class="hs-identifier hs-var">rep_ty</span></a></span></span><span> </span><span class="hs-glyph">=</span><span> </span><span class="hs-keyword">do</span><span>
</span><span id="line-388"></span><span>  </span><span class="annot"><a href="GHC.Tc.Deriv.Utils.html#DerivEnv"><span class="hs-identifier hs-type">DerivEnv</span></a></span><span> </span><span class="hs-special">{</span><span> </span><span class="annot"><span class="annottext">denv_tvs :: DerivEnv -&gt; [TcTyVar]
</span><a href="GHC.Tc.Deriv.Utils.html#denv_tvs"><span class="hs-identifier hs-var">denv_tvs</span></a></span><span>      </span><span class="hs-glyph">=</span><span> </span><span id="local-6989586621681174285"><span class="annot"><span class="annottext">[TcTyVar]
</span><a href="#local-6989586621681174285"><span class="hs-identifier hs-var">tvs</span></a></span></span><span>
</span><span id="line-389"></span><span>           </span><span class="hs-special">,</span><span> </span><span class="annot"><span class="annottext">denv_cls :: DerivEnv -&gt; Class
</span><a href="GHC.Tc.Deriv.Utils.html#denv_cls"><span class="hs-identifier hs-var">denv_cls</span></a></span><span>      </span><span class="hs-glyph">=</span><span> </span><span id="local-6989586621681174284"><span class="annot"><span class="annottext">Class
</span><a href="#local-6989586621681174284"><span class="hs-identifier hs-var">cls</span></a></span></span><span>
</span><span id="line-390"></span><span>           </span><span class="hs-special">,</span><span> </span><span class="annot"><span class="annottext">denv_inst_tys :: DerivEnv -&gt; ThetaType
</span><a href="GHC.Tc.Deriv.Utils.html#denv_inst_tys"><span class="hs-identifier hs-var">denv_inst_tys</span></a></span><span> </span><span class="hs-glyph">=</span><span> </span><span id="local-6989586621681174283"><span class="annot"><span class="annottext">ThetaType
</span><a href="#local-6989586621681174283"><span class="hs-identifier hs-var">inst_tys</span></a></span></span><span> </span><span class="hs-special">}</span><span> </span><span class="hs-glyph">&lt;-</span><span> </span><span class="annot"><span class="annottext">ReaderT DerivEnv (IOEnv (Env TcGblEnv TcLclEnv)) DerivEnv
forall (m :: * -&gt; *) r. Monad m =&gt; ReaderT r m r
</span><a href="../../transformers/src/Control.Monad.Trans.Reader.html#ask"><span class="hs-identifier hs-var">ask</span></a></span><span>
</span><span id="line-391"></span><span>  </span><span id="local-6989586621681174282"><span class="annot"><span class="annottext">Bool
</span><a href="#local-6989586621681174282"><span class="hs-identifier hs-var">sa_wildcard</span></a></span></span><span> </span><span class="hs-glyph">&lt;-</span><span> </span><span class="annot"><span class="annottext">DerivM Bool
</span><a href="GHC.Tc.Deriv.Utils.html#isStandaloneWildcardDeriv"><span class="hs-identifier hs-var">isStandaloneWildcardDeriv</span></a></span><span>
</span><span id="line-392"></span><span>  </span><span class="hs-keyword">let</span><span> </span><span class="hs-comment">-- The following functions are polymorphic over the representation</span><span>
</span><span id="line-393"></span><span>      </span><span class="hs-comment">-- type, since we might either give it the underlying type of a</span><span>
</span><span id="line-394"></span><span>      </span><span class="hs-comment">-- newtype (for GeneralizedNewtypeDeriving) or a @via@ type</span><span>
</span><span id="line-395"></span><span>      </span><span class="hs-comment">-- (for DerivingVia).</span><span>
</span><span id="line-396"></span><span>      </span><span id="local-6989586621681174281"><span class="annot"><span class="annottext">rep_tys :: PredType -&gt; ThetaType
</span><a href="#local-6989586621681174281"><span class="hs-identifier hs-var hs-var">rep_tys</span></a></span></span><span> </span><span id="local-6989586621681174280"><span class="annot"><span class="annottext">PredType
</span><a href="#local-6989586621681174280"><span class="hs-identifier hs-var">ty</span></a></span></span><span>  </span><span class="hs-glyph">=</span><span> </span><span class="annot"><span class="annottext">ThetaType
</span><a href="#local-6989586621681174287"><span class="hs-identifier hs-var">cls_tys</span></a></span><span> </span><span class="annot"><span class="annottext">ThetaType -&gt; ThetaType -&gt; ThetaType
forall a. [a] -&gt; [a] -&gt; [a]
</span><a href="../../base/src/GHC.Base.html#%2B%2B"><span class="hs-operator hs-var">++</span></a></span><span> </span><span class="hs-special">[</span><span class="annot"><span class="annottext">PredType
</span><a href="#local-6989586621681174280"><span class="hs-identifier hs-var">ty</span></a></span><span class="hs-special">]</span><span>
</span><span id="line-397"></span><span>      </span><span id="local-6989586621681174279"><span class="annot"><span class="annottext">rep_pred :: PredType -&gt; PredType
</span><a href="#local-6989586621681174279"><span class="hs-identifier hs-var hs-var">rep_pred</span></a></span></span><span> </span><span id="local-6989586621681174278"><span class="annot"><span class="annottext">PredType
</span><a href="#local-6989586621681174278"><span class="hs-identifier hs-var">ty</span></a></span></span><span> </span><span class="hs-glyph">=</span><span> </span><span class="annot"><span class="annottext">Class -&gt; ThetaType -&gt; PredType
</span><a href="GHC.Core.Predicate.html#mkClassPred"><span class="hs-identifier hs-var">mkClassPred</span></a></span><span> </span><span class="annot"><span class="annottext">Class
</span><a href="#local-6989586621681174284"><span class="hs-identifier hs-var">cls</span></a></span><span> </span><span class="hs-special">(</span><span class="annot"><span class="annottext">PredType -&gt; ThetaType
</span><a href="#local-6989586621681174281"><span class="hs-identifier hs-var">rep_tys</span></a></span><span> </span><span class="annot"><span class="annottext">PredType
</span><a href="#local-6989586621681174278"><span class="hs-identifier hs-var">ty</span></a></span><span class="hs-special">)</span><span>
</span><span id="line-398"></span><span>      </span><span id="local-6989586621681174277"><span class="annot"><span class="annottext">rep_pred_o :: PredType -&gt; PredOrigin
</span><a href="#local-6989586621681174277"><span class="hs-identifier hs-var hs-var">rep_pred_o</span></a></span></span><span> </span><span id="local-6989586621681174276"><span class="annot"><span class="annottext">PredType
</span><a href="#local-6989586621681174276"><span class="hs-identifier hs-var">ty</span></a></span></span><span> </span><span class="hs-glyph">=</span><span> </span><span class="annot"><span class="annottext">CtOrigin -&gt; TypeOrKind -&gt; PredType -&gt; PredOrigin
</span><a href="GHC.Tc.Deriv.Utils.html#mkPredOrigin"><span class="hs-identifier hs-var">mkPredOrigin</span></a></span><span> </span><span class="annot"><span class="annottext">CtOrigin
</span><a href="#local-6989586621681174275"><span class="hs-identifier hs-var">deriv_origin</span></a></span><span> </span><span class="annot"><span class="annottext">TypeOrKind
</span><a href="GHC.Types.Basic.html#TypeLevel"><span class="hs-identifier hs-var">TypeLevel</span></a></span><span> </span><span class="hs-special">(</span><span class="annot"><span class="annottext">PredType -&gt; PredType
</span><a href="#local-6989586621681174279"><span class="hs-identifier hs-var">rep_pred</span></a></span><span> </span><span class="annot"><span class="annottext">PredType
</span><a href="#local-6989586621681174276"><span class="hs-identifier hs-var">ty</span></a></span><span class="hs-special">)</span><span>
</span><span id="line-399"></span><span>              </span><span class="hs-comment">-- rep_pred is the representation dictionary, from where</span><span>
</span><span id="line-400"></span><span>              </span><span class="hs-comment">-- we are going to get all the methods for the final</span><span>
</span><span id="line-401"></span><span>              </span><span class="hs-comment">-- dictionary</span><span>
</span><span id="line-402"></span><span>      </span><span id="local-6989586621681174275"><span class="annot"><span class="annottext">deriv_origin :: CtOrigin
</span><a href="#local-6989586621681174275"><span class="hs-identifier hs-var hs-var">deriv_origin</span></a></span></span><span> </span><span class="hs-glyph">=</span><span> </span><span class="annot"><span class="annottext">Bool -&gt; CtOrigin
</span><a href="GHC.Tc.Deriv.Utils.html#mkDerivOrigin"><span class="hs-identifier hs-var">mkDerivOrigin</span></a></span><span> </span><span class="annot"><span class="annottext">Bool
</span><a href="#local-6989586621681174282"><span class="hs-identifier hs-var">sa_wildcard</span></a></span><span>
</span><span id="line-403"></span><span>
</span><span id="line-404"></span><span>      </span><span class="hs-comment">-- Next we collect constraints for the class methods</span><span>
</span><span id="line-405"></span><span>      </span><span class="hs-comment">-- If there are no methods, we don't need any constraints</span><span>
</span><span id="line-406"></span><span>      </span><span class="hs-comment">-- Otherwise we need (C rep_ty), for the representation methods,</span><span>
</span><span id="line-407"></span><span>      </span><span class="hs-comment">-- and constraints to coerce each individual method</span><span>
</span><span id="line-408"></span><span>      </span><span class="annot"><a href="#local-6989586621681174274"><span class="hs-identifier hs-type">meth_preds</span></a></span><span> </span><span class="hs-glyph">::</span><span> </span><span class="annot"><a href="GHC.Core.TyCo.Rep.html#Type"><span class="hs-identifier hs-type">Type</span></a></span><span> </span><span class="hs-glyph">-&gt;</span><span> </span><span class="hs-special">[</span><span class="annot"><a href="GHC.Tc.Deriv.Utils.html#PredOrigin"><span class="hs-identifier hs-type">PredOrigin</span></a></span><span class="hs-special">]</span><span>
</span><span id="line-409"></span><span>      </span><span id="local-6989586621681174274"><span class="annot"><span class="annottext">meth_preds :: PredType -&gt; [PredOrigin]
</span><a href="#local-6989586621681174274"><span class="hs-identifier hs-var hs-var">meth_preds</span></a></span></span><span> </span><span id="local-6989586621681174273"><span class="annot"><span class="annottext">PredType
</span><a href="#local-6989586621681174273"><span class="hs-identifier hs-var">ty</span></a></span></span><span>
</span><span id="line-410"></span><span>        </span><span class="hs-glyph">|</span><span> </span><span class="annot"><span class="annottext">[TcTyVar] -&gt; Bool
forall (t :: * -&gt; *) a. Foldable t =&gt; t a -&gt; Bool
</span><a href="../../base/src/Data.Foldable.html#null"><span class="hs-identifier hs-var">null</span></a></span><span> </span><span class="annot"><span class="annottext">[TcTyVar]
</span><a href="#local-6989586621681174271"><span class="hs-identifier hs-var">meths</span></a></span><span> </span><span class="hs-glyph">=</span><span> </span><span class="hs-special">[</span><span class="hs-special">]</span><span> </span><span class="hs-comment">-- No methods =&gt; no constraints</span><span>
</span><span id="line-411"></span><span>                          </span><span class="hs-comment">-- (#12814)</span><span>
</span><span id="line-412"></span><span>        </span><span class="hs-glyph">|</span><span> </span><span class="annot"><span class="annottext">Bool
</span><a href="../../base/src/GHC.Base.html#otherwise"><span class="hs-identifier hs-var">otherwise</span></a></span><span> </span><span class="hs-glyph">=</span><span> </span><span class="annot"><span class="annottext">PredType -&gt; PredOrigin
</span><a href="#local-6989586621681174277"><span class="hs-identifier hs-var">rep_pred_o</span></a></span><span> </span><span class="annot"><span class="annottext">PredType
</span><a href="#local-6989586621681174273"><span class="hs-identifier hs-var">ty</span></a></span><span> </span><span class="annot"><span class="annottext">PredOrigin -&gt; [PredOrigin] -&gt; [PredOrigin]
forall a. a -&gt; [a] -&gt; [a]
</span><span class="hs-glyph hs-var">:</span></span><span> </span><span class="annot"><span class="annottext">PredType -&gt; [PredOrigin]
</span><a href="#local-6989586621681174270"><span class="hs-identifier hs-var">coercible_constraints</span></a></span><span> </span><span class="annot"><span class="annottext">PredType
</span><a href="#local-6989586621681174273"><span class="hs-identifier hs-var">ty</span></a></span><span>
</span><span id="line-413"></span><span>      </span><span id="local-6989586621681174271"><span class="annot"><span class="annottext">meths :: [TcTyVar]
</span><a href="#local-6989586621681174271"><span class="hs-identifier hs-var hs-var">meths</span></a></span></span><span> </span><span class="hs-glyph">=</span><span> </span><span class="annot"><span class="annottext">Class -&gt; [TcTyVar]
</span><a href="GHC.Core.Class.html#classMethods"><span class="hs-identifier hs-var">classMethods</span></a></span><span> </span><span class="annot"><span class="annottext">Class
</span><a href="#local-6989586621681174284"><span class="hs-identifier hs-var">cls</span></a></span><span>
</span><span id="line-414"></span><span>      </span><span id="local-6989586621681174270"><span class="annot"><span class="annottext">coercible_constraints :: PredType -&gt; [PredOrigin]
</span><a href="#local-6989586621681174270"><span class="hs-identifier hs-var hs-var">coercible_constraints</span></a></span></span><span> </span><span id="local-6989586621681174268"><span class="annot"><span class="annottext">PredType
</span><a href="#local-6989586621681174268"><span class="hs-identifier hs-var">ty</span></a></span></span><span>
</span><span id="line-415"></span><span>        </span><span class="hs-glyph">=</span><span> </span><span class="hs-special">[</span><span> </span><span class="annot"><span class="annottext">CtOrigin -&gt; TypeOrKind -&gt; PredType -&gt; PredOrigin
</span><a href="GHC.Tc.Deriv.Utils.html#mkPredOrigin"><span class="hs-identifier hs-var">mkPredOrigin</span></a></span><span> </span><span class="hs-special">(</span><span class="annot"><span class="annottext">TcTyVar -&gt; PredType -&gt; PredType -&gt; Bool -&gt; CtOrigin
</span><a href="GHC.Tc.Types.Origin.html#DerivOriginCoerce"><span class="hs-identifier hs-var">DerivOriginCoerce</span></a></span><span> </span><span class="annot"><span class="annottext">TcTyVar
</span><a href="#local-6989586621681174266"><span class="hs-identifier hs-var">meth</span></a></span><span> </span><span class="annot"><span class="annottext">PredType
</span><a href="#local-6989586621681174265"><span class="hs-identifier hs-var">t1</span></a></span><span> </span><span class="annot"><span class="annottext">PredType
</span><a href="#local-6989586621681174264"><span class="hs-identifier hs-var">t2</span></a></span><span> </span><span class="annot"><span class="annottext">Bool
</span><a href="#local-6989586621681174282"><span class="hs-identifier hs-var">sa_wildcard</span></a></span><span class="hs-special">)</span><span>
</span><span id="line-416"></span><span>                         </span><span class="annot"><span class="annottext">TypeOrKind
</span><a href="GHC.Types.Basic.html#TypeLevel"><span class="hs-identifier hs-var">TypeLevel</span></a></span><span> </span><span class="hs-special">(</span><span class="annot"><span class="annottext">PredType -&gt; PredType -&gt; PredType
</span><a href="GHC.Core.Coercion.html#mkReprPrimEqPred"><span class="hs-identifier hs-var">mkReprPrimEqPred</span></a></span><span> </span><span class="annot"><span class="annottext">PredType
</span><a href="#local-6989586621681174265"><span class="hs-identifier hs-var">t1</span></a></span><span> </span><span class="annot"><span class="annottext">PredType
</span><a href="#local-6989586621681174264"><span class="hs-identifier hs-var">t2</span></a></span><span class="hs-special">)</span><span>
</span><span id="line-417"></span><span>          </span><span class="hs-glyph">|</span><span> </span><span id="local-6989586621681174266"><span class="annot"><span class="annottext">TcTyVar
</span><a href="#local-6989586621681174266"><span class="hs-identifier hs-var">meth</span></a></span></span><span> </span><span class="hs-glyph">&lt;-</span><span> </span><span class="annot"><span class="annottext">[TcTyVar]
</span><a href="#local-6989586621681174271"><span class="hs-identifier hs-var">meths</span></a></span><span>
</span><span id="line-418"></span><span>          </span><span class="hs-special">,</span><span> </span><span class="hs-keyword">let</span><span> </span><span class="hs-special">(</span><span class="annot"><a href="GHC.Data.Pair.html#Pair"><span class="hs-identifier hs-type">Pair</span></a></span><span> </span><span id="local-6989586621681174265"><span class="annot"><span class="annottext">PredType
</span><a href="#local-6989586621681174265"><span class="hs-identifier hs-var">t1</span></a></span></span><span> </span><span id="local-6989586621681174264"><span class="annot"><span class="annottext">PredType
</span><a href="#local-6989586621681174264"><span class="hs-identifier hs-var">t2</span></a></span></span><span class="hs-special">)</span><span> </span><span class="hs-glyph">=</span><span> </span><span class="annot"><span class="annottext">Class
-&gt; [TcTyVar] -&gt; ThetaType -&gt; PredType -&gt; TcTyVar -&gt; Pair PredType
</span><a href="GHC.Tc.Deriv.Generate.html#mkCoerceClassMethEqn"><span class="hs-identifier hs-var">mkCoerceClassMethEqn</span></a></span><span> </span><span class="annot"><span class="annottext">Class
</span><a href="#local-6989586621681174284"><span class="hs-identifier hs-var">cls</span></a></span><span> </span><span class="annot"><span class="annottext">[TcTyVar]
</span><a href="#local-6989586621681174285"><span class="hs-identifier hs-var">tvs</span></a></span><span>
</span><span id="line-419"></span><span>                                       </span><span class="annot"><span class="annottext">ThetaType
</span><a href="#local-6989586621681174283"><span class="hs-identifier hs-var">inst_tys</span></a></span><span> </span><span class="annot"><span class="annottext">PredType
</span><a href="#local-6989586621681174268"><span class="hs-identifier hs-var">ty</span></a></span><span> </span><span class="annot"><span class="annottext">TcTyVar
</span><a href="#local-6989586621681174266"><span class="hs-identifier hs-var">meth</span></a></span><span> </span><span class="hs-special">]</span><span>
</span><span id="line-420"></span><span>
</span><span id="line-421"></span><span>      </span><span class="annot"><a href="#local-6989586621681174260"><span class="hs-identifier hs-type">all_thetas</span></a></span><span> </span><span class="hs-glyph">::</span><span> </span><span class="annot"><a href="GHC.Core.TyCo.Rep.html#Type"><span class="hs-identifier hs-type">Type</span></a></span><span> </span><span class="hs-glyph">-&gt;</span><span> </span><span class="hs-special">[</span><span class="annot"><a href="GHC.Tc.Deriv.Utils.html#ThetaOrigin"><span class="hs-identifier hs-type">ThetaOrigin</span></a></span><span class="hs-special">]</span><span>
</span><span id="line-422"></span><span>      </span><span id="local-6989586621681174260"><span class="annot"><span class="annottext">all_thetas :: PredType -&gt; [ThetaOrigin]
</span><a href="#local-6989586621681174260"><span class="hs-identifier hs-var hs-var">all_thetas</span></a></span></span><span> </span><span id="local-6989586621681174259"><span class="annot"><span class="annottext">PredType
</span><a href="#local-6989586621681174259"><span class="hs-identifier hs-var">ty</span></a></span></span><span> </span><span class="hs-glyph">=</span><span> </span><span class="hs-special">[</span><span class="annot"><span class="annottext">[PredOrigin] -&gt; ThetaOrigin
</span><a href="GHC.Tc.Deriv.Utils.html#mkThetaOriginFromPreds"><span class="hs-identifier hs-var">mkThetaOriginFromPreds</span></a></span><span> </span><span class="annot"><span class="annottext">([PredOrigin] -&gt; ThetaOrigin) -&gt; [PredOrigin] -&gt; ThetaOrigin
forall a b. (a -&gt; b) -&gt; a -&gt; b
</span><a href="../../base/src/GHC.Base.html#%24"><span class="hs-operator hs-var">$</span></a></span><span> </span><span class="annot"><span class="annottext">PredType -&gt; [PredOrigin]
</span><a href="#local-6989586621681174274"><span class="hs-identifier hs-var">meth_preds</span></a></span><span> </span><span class="annot"><span class="annottext">PredType
</span><a href="#local-6989586621681174259"><span class="hs-identifier hs-var">ty</span></a></span><span class="hs-special">]</span><span>
</span><span id="line-423"></span><span>
</span><span id="line-424"></span><span>  </span><span class="annot"><span class="annottext">[ThetaOrigin] -&gt; DerivM [ThetaOrigin]
forall (f :: * -&gt; *) a. Applicative f =&gt; a -&gt; f a
</span><a href="../../base/src/GHC.Base.html#pure"><span class="hs-identifier hs-var">pure</span></a></span><span> </span><span class="hs-special">(</span><span class="annot"><span class="annottext">PredType -&gt; [ThetaOrigin]
</span><a href="#local-6989586621681174260"><span class="hs-identifier hs-var">all_thetas</span></a></span><span> </span><span class="annot"><span class="annottext">PredType
</span><a href="#local-6989586621681174286"><span class="hs-identifier hs-var">rep_ty</span></a></span><span class="hs-special">)</span><span>
</span><span id="line-425"></span><span>
</span><span id="line-426"></span><span class="hs-comment">{- Note [Inferring the instance context]
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
There are two sorts of 'deriving', as represented by the two constructors
for DerivContext:

  * InferContext mb_wildcard: This can either be:
    - The deriving clause for a data type.
        (e.g, data T a = T1 a deriving( Eq ))
      In this case, mb_wildcard = Nothing.
    - A standalone declaration with an extra-constraints wildcard
        (e.g., deriving instance _ =&gt; Eq (Foo a))
      In this case, mb_wildcard = Just loc, where loc is the location
      of the extra-constraints wildcard.

    Here we must infer an instance context,
    and generate instance declaration
      instance Eq a =&gt; Eq (T a) where ...

  * SupplyContext theta: standalone deriving
      deriving instance Eq a =&gt; Eq (T a)
    Here we only need to fill in the bindings;
    the instance context (theta) is user-supplied

For the InferContext case, we must figure out the
instance context (inferConstraintsStock). Suppose we are inferring
the instance context for
    C t1 .. tn (T s1 .. sm)
There are two cases

  * (T s1 .. sm) :: *         (the normal case)
    Then we behave like Eq and guess (C t1 .. tn t)
    for each data constructor arg of type t.  More
    details below.

  * (T s1 .. sm) :: * -&gt; *    (the functor-like case)
    Then we behave like Functor.

In both cases we produce a bunch of un-simplified constraints
and them simplify them in simplifyInstanceContexts; see
Note [Simplifying the instance context].

In the functor-like case, we may need to unify some kind variables with * in
order for the generated instance to be well-kinded. An example from
#10524:

  newtype Compose (f :: k2 -&gt; *) (g :: k1 -&gt; k2) (a :: k1)
    = Compose (f (g a)) deriving Functor

Earlier in the deriving pipeline, GHC unifies the kind of Compose f g
(k1 -&gt; *) with the kind of Functor's argument (* -&gt; *), so k1 := *. But this
alone isn't enough, since k2 wasn't unified with *:

  instance (Functor (f :: k2 -&gt; *), Functor (g :: * -&gt; k2)) =&gt;
    Functor (Compose f g) where ...

The two Functor constraints are ill-kinded. To ensure this doesn't happen, we:

  1. Collect all of a datatype's subtypes which require functor-like
     constraints.
  2. For each subtype, create a substitution by unifying the subtype's kind
     with (* -&gt; *).
  3. Compose all the substitutions into one, then apply that substitution to
     all of the in-scope type variables and the instance types.

Note [Getting base classes]
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
Functor and Typeable are defined in package 'base', and that is not available
when compiling 'ghc-prim'.  So we must be careful that 'deriving' for stuff in
ghc-prim does not use Functor or Typeable implicitly via these lookups.

Note [Deriving and unboxed types]
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
We have some special hacks to support things like
   data T = MkT Int# deriving ( Show )

Specifically, we use GHC.Tc.Deriv.Generate.box to box the Int# into an Int
(which we know how to show), and append a '#'. Parentheses are not required
for unboxed values (`MkT -3#` is a valid expression).

Note [Superclasses of derived instance]
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
In general, a derived instance decl needs the superclasses of the derived
class too.  So if we have
        data T a = ...deriving( Ord )
then the initial context for Ord (T a) should include Eq (T a).  Often this is
redundant; we'll also generate an Ord constraint for each constructor argument,
and that will probably generate enough constraints to make the Eq (T a) constraint
be satisfied too.  But not always; consider:

 data S a = S
 instance Eq (S a)
 instance Ord (S a)

 data T a = MkT (S a) deriving( Ord )
 instance Num a =&gt; Eq (T a)

The derived instance for (Ord (T a)) must have a (Num a) constraint!
Similarly consider:
        data T a = MkT deriving( Data )
Here there *is* no argument field, but we must nevertheless generate
a context for the Data instances:
        instance Typeable a =&gt; Data (T a) where ...


************************************************************************
*                                                                      *
         Finding the fixed point of deriving equations
*                                                                      *
************************************************************************

Note [Simplifying the instance context]
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
Consider

        data T a b = C1 (Foo a) (Bar b)
                   | C2 Int (T b a)
                   | C3 (T a a)
                   deriving (Eq)

We want to come up with an instance declaration of the form

        instance (Ping a, Pong b, ...) =&gt; Eq (T a b) where
                x == y = ...

It is pretty easy, albeit tedious, to fill in the code &quot;...&quot;.  The
trick is to figure out what the context for the instance decl is,
namely Ping, Pong and friends.

Let's call the context reqd for the T instance of class C at types
(a,b, ...)  C (T a b).  Thus:

        Eq (T a b) = (Ping a, Pong b, ...)

Now we can get a (recursive) equation from the data decl.  This part
is done by inferConstraintsStock.

        Eq (T a b) = Eq (Foo a) u Eq (Bar b)    -- From C1
                   u Eq (T b a) u Eq Int        -- From C2
                   u Eq (T a a)                 -- From C3


Foo and Bar may have explicit instances for Eq, in which case we can
just substitute for them.  Alternatively, either or both may have
their Eq instances given by deriving clauses, in which case they
form part of the system of equations.

Now all we need do is simplify and solve the equations, iterating to
find the least fixpoint.  This is done by simplifyInstanceConstraints.
Notice that the order of the arguments can
switch around, as here in the recursive calls to T.

Let's suppose Eq (Foo a) = Eq a, and Eq (Bar b) = Ping b.

We start with:

        Eq (T a b) = {}         -- The empty set

Next iteration:
        Eq (T a b) = Eq (Foo a) u Eq (Bar b)    -- From C1
                   u Eq (T b a) u Eq Int        -- From C2
                   u Eq (T a a)                 -- From C3

        After simplification:
                   = Eq a u Ping b u {} u {} u {}
                   = Eq a u Ping b

Next iteration:

        Eq (T a b) = Eq (Foo a) u Eq (Bar b)    -- From C1
                   u Eq (T b a) u Eq Int        -- From C2
                   u Eq (T a a)                 -- From C3

        After simplification:
                   = Eq a u Ping b
                   u (Eq b u Ping a)
                   u (Eq a u Ping a)

                   = Eq a u Ping b u Eq b u Ping a

The next iteration gives the same result, so this is the fixpoint.  We
need to make a canonical form of the RHS to ensure convergence.  We do
this by simplifying the RHS to a form in which

        - the classes constrain only tyvars
        - the list is sorted by tyvar (major key) and then class (minor key)
        - no duplicates, of course

Note [Deterministic simplifyInstanceContexts]
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
Canonicalisation uses nonDetCmpType which is nondeterministic. Sorting
with nonDetCmpType puts the returned lists in a nondeterministic order.
If we were to return them, we'd get class constraints in
nondeterministic order.

Consider:

  data ADT a b = Z a b deriving Eq

The generated code could be either:

  instance (Eq a, Eq b) =&gt; Eq (Z a b) where

Or:

  instance (Eq b, Eq a) =&gt; Eq (Z a b) where

To prevent the order from being nondeterministic we only
canonicalize when comparing and return them in the same order as
simplifyDeriv returned them.
See also Note [nonDetCmpType nondeterminism]
-}</span><span>
</span><span id="line-637"></span><span>
</span><span id="line-638"></span><span>
</span><span id="line-639"></span><span class="annot"><a href="GHC.Tc.Deriv.Infer.html#simplifyInstanceContexts"><span class="hs-identifier hs-type">simplifyInstanceContexts</span></a></span><span> </span><span class="hs-glyph">::</span><span> </span><span class="hs-special">[</span><span class="annot"><a href="GHC.Tc.Deriv.Utils.html#DerivSpec"><span class="hs-identifier hs-type">DerivSpec</span></a></span><span> </span><span class="hs-special">[</span><span class="annot"><a href="GHC.Tc.Deriv.Utils.html#ThetaOrigin"><span class="hs-identifier hs-type">ThetaOrigin</span></a></span><span class="hs-special">]</span><span class="hs-special">]</span><span>
</span><span id="line-640"></span><span>                         </span><span class="hs-glyph">-&gt;</span><span> </span><span class="annot"><a href="GHC.Tc.Types.html#TcM"><span class="hs-identifier hs-type">TcM</span></a></span><span> </span><span class="hs-special">[</span><span class="annot"><a href="GHC.Tc.Deriv.Utils.html#DerivSpec"><span class="hs-identifier hs-type">DerivSpec</span></a></span><span> </span><span class="annot"><a href="GHC.Core.TyCo.Rep.html#ThetaType"><span class="hs-identifier hs-type">ThetaType</span></a></span><span class="hs-special">]</span><span>
</span><span id="line-641"></span><span class="hs-comment">-- Used only for deriving clauses or standalone deriving with an</span><span>
</span><span id="line-642"></span><span class="hs-comment">-- extra-constraints wildcard (InferContext)</span><span>
</span><span id="line-643"></span><span class="hs-comment">-- See Note [Simplifying the instance context]</span><span>
</span><span id="line-644"></span><span>
</span><span id="line-645"></span><span id="simplifyInstanceContexts"><span class="annot"><span class="annottext">simplifyInstanceContexts :: [DerivSpec [ThetaOrigin]] -&gt; TcM [DerivSpec ThetaType]
</span><a href="GHC.Tc.Deriv.Infer.html#simplifyInstanceContexts"><span class="hs-identifier hs-var hs-var">simplifyInstanceContexts</span></a></span></span><span> </span><span class="hs-special">[</span><span class="hs-special">]</span><span> </span><span class="hs-glyph">=</span><span> </span><span class="annot"><span class="annottext">[DerivSpec ThetaType] -&gt; TcM [DerivSpec ThetaType]
forall (m :: * -&gt; *) a. Monad m =&gt; a -&gt; m a
</span><a href="../../base/src/GHC.Base.html#return"><span class="hs-identifier hs-var">return</span></a></span><span> </span><span class="hs-special">[</span><span class="hs-special">]</span><span>
</span><span id="line-646"></span><span>
</span><span id="line-647"></span><span class="annot"><a href="GHC.Tc.Deriv.Infer.html#simplifyInstanceContexts"><span class="hs-identifier hs-var">simplifyInstanceContexts</span></a></span><span> </span><span id="local-6989586621681174258"><span class="annot"><span class="annottext">[DerivSpec [ThetaOrigin]]
</span><a href="#local-6989586621681174258"><span class="hs-identifier hs-var">infer_specs</span></a></span></span><span>
</span><span id="line-648"></span><span>  </span><span class="hs-glyph">=</span><span> </span><span class="hs-keyword">do</span><span>  </span><span class="hs-special">{</span><span> </span><span class="annot"><span class="annottext">String -&gt; SDoc -&gt; IOEnv (Env TcGblEnv TcLclEnv) ()
</span><a href="GHC.Tc.Utils.Monad.html#traceTc"><span class="hs-identifier hs-var">traceTc</span></a></span><span> </span><span class="annot"><span class="annottext">String
</span><span class="hs-string">&quot;simplifyInstanceContexts&quot;</span></span><span> </span><span class="annot"><span class="annottext">(SDoc -&gt; IOEnv (Env TcGblEnv TcLclEnv) ())
-&gt; SDoc -&gt; IOEnv (Env TcGblEnv TcLclEnv) ()
forall a b. (a -&gt; b) -&gt; a -&gt; b
</span><a href="../../base/src/GHC.Base.html#%24"><span class="hs-operator hs-var">$</span></a></span><span> </span><span class="annot"><span class="annottext">[SDoc] -&gt; SDoc
</span><a href="GHC.Utils.Outputable.html#vcat"><span class="hs-identifier hs-var">vcat</span></a></span><span> </span><span class="hs-special">(</span><span class="annot"><span class="annottext">(DerivSpec [ThetaOrigin] -&gt; SDoc)
-&gt; [DerivSpec [ThetaOrigin]] -&gt; [SDoc]
forall a b. (a -&gt; b) -&gt; [a] -&gt; [b]
</span><a href="../../base/src/GHC.Base.html#map"><span class="hs-identifier hs-var">map</span></a></span><span> </span><span class="annot"><span class="annottext">DerivSpec [ThetaOrigin] -&gt; SDoc
forall theta. Outputable theta =&gt; DerivSpec theta -&gt; SDoc
</span><a href="GHC.Tc.Deriv.Utils.html#pprDerivSpec"><span class="hs-identifier hs-var">pprDerivSpec</span></a></span><span> </span><span class="annot"><span class="annottext">[DerivSpec [ThetaOrigin]]
</span><a href="#local-6989586621681174258"><span class="hs-identifier hs-var">infer_specs</span></a></span><span class="hs-special">)</span><span>
</span><span id="line-649"></span><span>        </span><span class="hs-special">;</span><span> </span><span class="annot"><span class="annottext">Int -&gt; [ThetaType] -&gt; TcM [DerivSpec ThetaType]
</span><a href="#local-6989586621681174256"><span class="hs-identifier hs-var">iterate_deriv</span></a></span><span> </span><span class="annot"><span class="annottext">Int
</span><span class="hs-number">1</span></span><span> </span><span class="annot"><span class="annottext">[ThetaType]
</span><a href="#local-6989586621681174255"><span class="hs-identifier hs-var">initial_solutions</span></a></span><span> </span><span class="hs-special">}</span><span>
</span><span id="line-650"></span><span>  </span><span class="hs-keyword">where</span><span>
</span><span id="line-651"></span><span>    </span><span class="hs-comment">------------------------------------------------------------------</span><span>
</span><span id="line-652"></span><span>        </span><span class="hs-comment">-- The initial solutions for the equations claim that each</span><span>
</span><span id="line-653"></span><span>        </span><span class="hs-comment">-- instance has an empty context; this solution is certainly</span><span>
</span><span id="line-654"></span><span>        </span><span class="hs-comment">-- in canonical form.</span><span>
</span><span id="line-655"></span><span>    </span><span class="annot"><a href="#local-6989586621681174255"><span class="hs-identifier hs-type">initial_solutions</span></a></span><span> </span><span class="hs-glyph">::</span><span> </span><span class="hs-special">[</span><span class="annot"><a href="GHC.Core.TyCo.Rep.html#ThetaType"><span class="hs-identifier hs-type">ThetaType</span></a></span><span class="hs-special">]</span><span>
</span><span id="line-656"></span><span>    </span><span id="local-6989586621681174255"><span class="annot"><span class="annottext">initial_solutions :: [ThetaType]
</span><a href="#local-6989586621681174255"><span class="hs-identifier hs-var hs-var">initial_solutions</span></a></span></span><span> </span><span class="hs-glyph">=</span><span> </span><span class="hs-special">[</span><span> </span><span class="hs-special">[</span><span class="hs-special">]</span><span> </span><span class="hs-glyph">|</span><span> </span><span class="annot"><span class="annottext">DerivSpec [ThetaOrigin]
</span><span class="hs-identifier">_</span></span><span> </span><span class="hs-glyph">&lt;-</span><span> </span><span class="annot"><span class="annottext">[DerivSpec [ThetaOrigin]]
</span><a href="#local-6989586621681174258"><span class="hs-identifier hs-var">infer_specs</span></a></span><span> </span><span class="hs-special">]</span><span>
</span><span id="line-657"></span><span>
</span><span id="line-658"></span><span>    </span><span class="hs-comment">------------------------------------------------------------------</span><span>
</span><span id="line-659"></span><span>        </span><span class="hs-comment">-- iterate_deriv calculates the next batch of solutions,</span><span>
</span><span id="line-660"></span><span>        </span><span class="hs-comment">-- compares it with the current one; finishes if they are the</span><span>
</span><span id="line-661"></span><span>        </span><span class="hs-comment">-- same, otherwise recurses with the new solutions.</span><span>
</span><span id="line-662"></span><span>        </span><span class="hs-comment">-- It fails if any iteration fails</span><span>
</span><span id="line-663"></span><span>    </span><span class="annot"><a href="#local-6989586621681174256"><span class="hs-identifier hs-type">iterate_deriv</span></a></span><span> </span><span class="hs-glyph">::</span><span> </span><span class="annot"><span class="hs-identifier hs-type">Int</span></span><span> </span><span class="hs-glyph">-&gt;</span><span> </span><span class="hs-special">[</span><span class="annot"><a href="GHC.Core.TyCo.Rep.html#ThetaType"><span class="hs-identifier hs-type">ThetaType</span></a></span><span class="hs-special">]</span><span> </span><span class="hs-glyph">-&gt;</span><span> </span><span class="annot"><a href="GHC.Tc.Types.html#TcM"><span class="hs-identifier hs-type">TcM</span></a></span><span> </span><span class="hs-special">[</span><span class="annot"><a href="GHC.Tc.Deriv.Utils.html#DerivSpec"><span class="hs-identifier hs-type">DerivSpec</span></a></span><span> </span><span class="annot"><a href="GHC.Core.TyCo.Rep.html#ThetaType"><span class="hs-identifier hs-type">ThetaType</span></a></span><span class="hs-special">]</span><span>
</span><span id="line-664"></span><span>    </span><span id="local-6989586621681174256"><span class="annot"><span class="annottext">iterate_deriv :: Int -&gt; [ThetaType] -&gt; TcM [DerivSpec ThetaType]
</span><a href="#local-6989586621681174256"><span class="hs-identifier hs-var hs-var">iterate_deriv</span></a></span></span><span> </span><span id="local-6989586621681174254"><span class="annot"><span class="annottext">Int
</span><a href="#local-6989586621681174254"><span class="hs-identifier hs-var">n</span></a></span></span><span> </span><span id="local-6989586621681174253"><span class="annot"><span class="annottext">[ThetaType]
</span><a href="#local-6989586621681174253"><span class="hs-identifier hs-var">current_solns</span></a></span></span><span>
</span><span id="line-665"></span><span>      </span><span class="hs-glyph">|</span><span> </span><span class="annot"><span class="annottext">Int
</span><a href="#local-6989586621681174254"><span class="hs-identifier hs-var">n</span></a></span><span> </span><span class="annot"><span class="annottext">Int -&gt; Int -&gt; Bool
forall a. Ord a =&gt; a -&gt; a -&gt; Bool
</span><span class="hs-operator hs-var">&gt;</span></span><span> </span><span class="annot"><span class="annottext">Int
</span><span class="hs-number">20</span></span><span>  </span><span class="hs-comment">-- Looks as if we are in an infinite loop</span><span>
</span><span id="line-666"></span><span>                </span><span class="hs-comment">-- This can happen if we have -XUndecidableInstances</span><span>
</span><span id="line-667"></span><span>                </span><span class="hs-comment">-- (See GHC.Tc.Solver.tcSimplifyDeriv.)</span><span>
</span><span id="line-668"></span><span>      </span><span class="hs-glyph">=</span><span> </span><span class="annot"><span class="annottext">String -&gt; SDoc -&gt; TcM [DerivSpec ThetaType]
forall a. HasCallStack =&gt; String -&gt; SDoc -&gt; a
</span><a href="GHC.Utils.Outputable.html#pprPanic"><span class="hs-identifier hs-var">pprPanic</span></a></span><span> </span><span class="annot"><span class="annottext">String
</span><span class="hs-string">&quot;solveDerivEqns: probable loop&quot;</span></span><span>
</span><span id="line-669"></span><span>                 </span><span class="hs-special">(</span><span class="annot"><span class="annottext">[SDoc] -&gt; SDoc
</span><a href="GHC.Utils.Outputable.html#vcat"><span class="hs-identifier hs-var">vcat</span></a></span><span> </span><span class="hs-special">(</span><span class="annot"><span class="annottext">(DerivSpec [ThetaOrigin] -&gt; SDoc)
-&gt; [DerivSpec [ThetaOrigin]] -&gt; [SDoc]
forall a b. (a -&gt; b) -&gt; [a] -&gt; [b]
</span><a href="../../base/src/GHC.Base.html#map"><span class="hs-identifier hs-var">map</span></a></span><span> </span><span class="annot"><span class="annottext">DerivSpec [ThetaOrigin] -&gt; SDoc
forall theta. Outputable theta =&gt; DerivSpec theta -&gt; SDoc
</span><a href="GHC.Tc.Deriv.Utils.html#pprDerivSpec"><span class="hs-identifier hs-var">pprDerivSpec</span></a></span><span> </span><span class="annot"><span class="annottext">[DerivSpec [ThetaOrigin]]
</span><a href="#local-6989586621681174258"><span class="hs-identifier hs-var">infer_specs</span></a></span><span class="hs-special">)</span><span> </span><span class="annot"><span class="annottext">SDoc -&gt; SDoc -&gt; SDoc
</span><a href="GHC.Utils.Outputable.html#%24%24"><span class="hs-operator hs-var">$$</span></a></span><span> </span><span class="annot"><span class="annottext">[ThetaType] -&gt; SDoc
forall a. Outputable a =&gt; a -&gt; SDoc
</span><a href="GHC.Utils.Outputable.html#ppr"><span class="hs-identifier hs-var">ppr</span></a></span><span> </span><span class="annot"><span class="annottext">[ThetaType]
</span><a href="#local-6989586621681174253"><span class="hs-identifier hs-var">current_solns</span></a></span><span class="hs-special">)</span><span>
</span><span id="line-670"></span><span>      </span><span class="hs-glyph">|</span><span> </span><span class="annot"><span class="annottext">Bool
</span><a href="../../base/src/GHC.Base.html#otherwise"><span class="hs-identifier hs-var">otherwise</span></a></span><span>
</span><span id="line-671"></span><span>      </span><span class="hs-glyph">=</span><span> </span><span class="hs-keyword">do</span><span> </span><span class="hs-special">{</span><span>      </span><span class="hs-comment">-- Extend the inst info from the explicit instance decls</span><span>
</span><span id="line-672"></span><span>                  </span><span class="hs-comment">-- with the current set of solutions, and simplify each RHS</span><span>
</span><span id="line-673"></span><span>             </span><span id="local-6989586621681174250"><span class="annot"><span class="annottext">[ClsInst]
</span><a href="#local-6989586621681174250"><span class="hs-identifier hs-var">inst_specs</span></a></span></span><span> </span><span class="hs-glyph">&lt;-</span><span> </span><span class="annot"><span class="annottext">(ThetaType
 -&gt; DerivSpec [ThetaOrigin]
 -&gt; IOEnv (Env TcGblEnv TcLclEnv) ClsInst)
-&gt; [ThetaType]
-&gt; [DerivSpec [ThetaOrigin]]
-&gt; IOEnv (Env TcGblEnv TcLclEnv) [ClsInst]
forall (m :: * -&gt; *) a b c.
Applicative m =&gt;
(a -&gt; b -&gt; m c) -&gt; [a] -&gt; [b] -&gt; m [c]
</span><a href="../../base/src/Control.Monad.html#zipWithM"><span class="hs-identifier hs-var">zipWithM</span></a></span><span> </span><span class="annot"><span class="annottext">ThetaType
-&gt; DerivSpec [ThetaOrigin] -&gt; IOEnv (Env TcGblEnv TcLclEnv) ClsInst
forall theta.
ThetaType
-&gt; DerivSpec theta -&gt; IOEnv (Env TcGblEnv TcLclEnv) ClsInst
</span><a href="GHC.Tc.Deriv.Utils.html#newDerivClsInst"><span class="hs-identifier hs-var">newDerivClsInst</span></a></span><span> </span><span class="annot"><span class="annottext">[ThetaType]
</span><a href="#local-6989586621681174253"><span class="hs-identifier hs-var">current_solns</span></a></span><span> </span><span class="annot"><span class="annottext">[DerivSpec [ThetaOrigin]]
</span><a href="#local-6989586621681174258"><span class="hs-identifier hs-var">infer_specs</span></a></span><span>
</span><span id="line-674"></span><span>           </span><span class="hs-special">;</span><span> </span><span id="local-6989586621681174247"><span class="annot"><span class="annottext">[ThetaType]
</span><a href="#local-6989586621681174247"><span class="hs-identifier hs-var">new_solns</span></a></span></span><span> </span><span class="hs-glyph">&lt;-</span><span> </span><span class="annot"><span class="annottext">TcM [ThetaType] -&gt; TcM [ThetaType]
forall r. TcM r -&gt; TcM r
</span><a href="GHC.Tc.Utils.Monad.html#checkNoErrs"><span class="hs-identifier hs-var">checkNoErrs</span></a></span><span> </span><span class="annot"><span class="annottext">(TcM [ThetaType] -&gt; TcM [ThetaType])
-&gt; TcM [ThetaType] -&gt; TcM [ThetaType]
forall a b. (a -&gt; b) -&gt; a -&gt; b
</span><a href="../../base/src/GHC.Base.html#%24"><span class="hs-operator hs-var">$</span></a></span><span>
</span><span id="line-675"></span><span>                          </span><span class="annot"><span class="annottext">[ClsInst] -&gt; TcM [ThetaType] -&gt; TcM [ThetaType]
forall a. [ClsInst] -&gt; TcM a -&gt; TcM a
</span><a href="GHC.Tc.Deriv.Utils.html#extendLocalInstEnv"><span class="hs-identifier hs-var">extendLocalInstEnv</span></a></span><span> </span><span class="annot"><span class="annottext">[ClsInst]
</span><a href="#local-6989586621681174250"><span class="hs-identifier hs-var">inst_specs</span></a></span><span> </span><span class="annot"><span class="annottext">(TcM [ThetaType] -&gt; TcM [ThetaType])
-&gt; TcM [ThetaType] -&gt; TcM [ThetaType]
forall a b. (a -&gt; b) -&gt; a -&gt; b
</span><a href="../../base/src/GHC.Base.html#%24"><span class="hs-operator hs-var">$</span></a></span><span>
</span><span id="line-676"></span><span>                          </span><span class="annot"><span class="annottext">(DerivSpec [ThetaOrigin]
 -&gt; IOEnv (Env TcGblEnv TcLclEnv) ThetaType)
-&gt; [DerivSpec [ThetaOrigin]] -&gt; TcM [ThetaType]
forall (t :: * -&gt; *) (m :: * -&gt; *) a b.
(Traversable t, Monad m) =&gt;
(a -&gt; m b) -&gt; t a -&gt; m (t b)
</span><a href="../../base/src/Data.Traversable.html#mapM"><span class="hs-identifier hs-var">mapM</span></a></span><span> </span><span class="annot"><span class="annottext">DerivSpec [ThetaOrigin] -&gt; IOEnv (Env TcGblEnv TcLclEnv) ThetaType
</span><a href="#local-6989586621681174244"><span class="hs-identifier hs-var">gen_soln</span></a></span><span> </span><span class="annot"><span class="annottext">[DerivSpec [ThetaOrigin]]
</span><a href="#local-6989586621681174258"><span class="hs-identifier hs-var">infer_specs</span></a></span><span>
</span><span id="line-677"></span><span>
</span><span id="line-678"></span><span>           </span><span class="hs-special">;</span><span> </span><span class="hs-keyword">if</span><span> </span><span class="hs-special">(</span><span class="annot"><span class="annottext">[ThetaType]
</span><a href="#local-6989586621681174253"><span class="hs-identifier hs-var">current_solns</span></a></span><span> </span><span class="annot"><span class="annottext">[ThetaType] -&gt; [ThetaType] -&gt; Bool
</span><a href="#local-6989586621681174243"><span class="hs-operator hs-var">`eqSolution`</span></a></span><span> </span><span class="annot"><span class="annottext">[ThetaType]
</span><a href="#local-6989586621681174247"><span class="hs-identifier hs-var">new_solns</span></a></span><span class="hs-special">)</span><span> </span><span class="hs-keyword">then</span><span>
</span><span id="line-679"></span><span>                </span><span class="annot"><span class="annottext">[DerivSpec ThetaType] -&gt; TcM [DerivSpec ThetaType]
forall (m :: * -&gt; *) a. Monad m =&gt; a -&gt; m a
</span><a href="../../base/src/GHC.Base.html#return"><span class="hs-identifier hs-var">return</span></a></span><span> </span><span class="hs-special">[</span><span> </span><span class="annot"><span class="annottext">DerivSpec [ThetaOrigin]
</span><a href="#local-6989586621681174242"><span class="hs-identifier hs-var">spec</span></a></span><span> </span><span class="hs-special">{</span><span> </span><span class="annot"><span class="annottext">ds_theta :: ThetaType
</span><a href="GHC.Tc.Deriv.Utils.html#ds_theta"><span class="hs-identifier hs-var">ds_theta</span></a></span><span> </span><span class="hs-glyph">=</span><span> </span><span class="annot"><span class="annottext">ThetaType
</span><a href="#local-6989586621681174240"><span class="hs-identifier hs-var">soln</span></a></span><span> </span><span class="hs-special">}</span><span>
</span><span id="line-680"></span><span>                       </span><span class="hs-glyph">|</span><span> </span><span class="hs-special">(</span><span id="local-6989586621681174242"><span class="annot"><span class="annottext">DerivSpec [ThetaOrigin]
</span><a href="#local-6989586621681174242"><span class="hs-identifier hs-var">spec</span></a></span></span><span class="hs-special">,</span><span> </span><span id="local-6989586621681174240"><span class="annot"><span class="annottext">ThetaType
</span><a href="#local-6989586621681174240"><span class="hs-identifier hs-var">soln</span></a></span></span><span class="hs-special">)</span><span> </span><span class="hs-glyph">&lt;-</span><span> </span><span class="annot"><span class="annottext">[DerivSpec [ThetaOrigin]]
-&gt; [ThetaType] -&gt; [(DerivSpec [ThetaOrigin], ThetaType)]
forall a b. [a] -&gt; [b] -&gt; [(a, b)]
</span><a href="../../base/src/GHC.List.html#zip"><span class="hs-identifier hs-var">zip</span></a></span><span> </span><span class="annot"><span class="annottext">[DerivSpec [ThetaOrigin]]
</span><a href="#local-6989586621681174258"><span class="hs-identifier hs-var">infer_specs</span></a></span><span> </span><span class="annot"><span class="annottext">[ThetaType]
</span><a href="#local-6989586621681174253"><span class="hs-identifier hs-var">current_solns</span></a></span><span> </span><span class="hs-special">]</span><span>
</span><span id="line-681"></span><span>             </span><span class="hs-keyword">else</span><span>
</span><span id="line-682"></span><span>                </span><span class="annot"><span class="annottext">Int -&gt; [ThetaType] -&gt; TcM [DerivSpec ThetaType]
</span><a href="#local-6989586621681174256"><span class="hs-identifier hs-var">iterate_deriv</span></a></span><span> </span><span class="hs-special">(</span><span class="annot"><span class="annottext">Int
</span><a href="#local-6989586621681174254"><span class="hs-identifier hs-var">n</span></a></span><span class="annot"><span class="annottext">Int -&gt; Int -&gt; Int
forall a. Num a =&gt; a -&gt; a -&gt; a
</span><a href="../../base/src/GHC.Num.html#%2B"><span class="hs-operator hs-var">+</span></a></span><span class="annot"><span class="annottext">Int
</span><span class="hs-number">1</span></span><span class="hs-special">)</span><span> </span><span class="annot"><span class="annottext">[ThetaType]
</span><a href="#local-6989586621681174247"><span class="hs-identifier hs-var">new_solns</span></a></span><span> </span><span class="hs-special">}</span><span>
</span><span id="line-683"></span><span>
</span><span id="line-684"></span><span>    </span><span id="local-6989586621681174243"><span class="annot"><span class="annottext">eqSolution :: [ThetaType] -&gt; [ThetaType] -&gt; Bool
</span><a href="#local-6989586621681174243"><span class="hs-identifier hs-var hs-var">eqSolution</span></a></span></span><span> </span><span id="local-6989586621681174238"><span class="annot"><span class="annottext">[ThetaType]
</span><a href="#local-6989586621681174238"><span class="hs-identifier hs-var">a</span></a></span></span><span> </span><span id="local-6989586621681174237"><span class="annot"><span class="annottext">[ThetaType]
</span><a href="#local-6989586621681174237"><span class="hs-identifier hs-var">b</span></a></span></span><span> </span><span class="hs-glyph">=</span><span> </span><span class="annot"><span class="annottext">(ThetaType -&gt; ThetaType -&gt; Bool)
-&gt; [ThetaType] -&gt; [ThetaType] -&gt; Bool
forall a. (a -&gt; a -&gt; Bool) -&gt; [a] -&gt; [a] -&gt; Bool
</span><a href="GHC.Utils.Misc.html#eqListBy"><span class="hs-identifier hs-var">eqListBy</span></a></span><span> </span><span class="hs-special">(</span><span class="annot"><span class="annottext">(PredType -&gt; PredType -&gt; Bool) -&gt; ThetaType -&gt; ThetaType -&gt; Bool
forall a. (a -&gt; a -&gt; Bool) -&gt; [a] -&gt; [a] -&gt; Bool
</span><a href="GHC.Utils.Misc.html#eqListBy"><span class="hs-identifier hs-var">eqListBy</span></a></span><span> </span><span class="annot"><span class="annottext">PredType -&gt; PredType -&gt; Bool
</span><a href="GHC.Core.Type.html#eqType"><span class="hs-identifier hs-var">eqType</span></a></span><span class="hs-special">)</span><span> </span><span class="hs-special">(</span><span class="annot"><span class="annottext">[ThetaType] -&gt; [ThetaType]
</span><a href="#local-6989586621681174234"><span class="hs-identifier hs-var">canSolution</span></a></span><span> </span><span class="annot"><span class="annottext">[ThetaType]
</span><a href="#local-6989586621681174238"><span class="hs-identifier hs-var">a</span></a></span><span class="hs-special">)</span><span> </span><span class="hs-special">(</span><span class="annot"><span class="annottext">[ThetaType] -&gt; [ThetaType]
</span><a href="#local-6989586621681174234"><span class="hs-identifier hs-var">canSolution</span></a></span><span> </span><span class="annot"><span class="annottext">[ThetaType]
</span><a href="#local-6989586621681174237"><span class="hs-identifier hs-var">b</span></a></span><span class="hs-special">)</span><span>
</span><span id="line-685"></span><span>       </span><span class="hs-comment">-- Canonicalise for comparison</span><span>
</span><span id="line-686"></span><span>       </span><span class="hs-comment">-- See Note [Deterministic simplifyInstanceContexts]</span><span>
</span><span id="line-687"></span><span>    </span><span id="local-6989586621681174234"><span class="annot"><span class="annottext">canSolution :: [ThetaType] -&gt; [ThetaType]
</span><a href="#local-6989586621681174234"><span class="hs-identifier hs-var hs-var">canSolution</span></a></span></span><span> </span><span class="hs-glyph">=</span><span> </span><span class="annot"><span class="annottext">(ThetaType -&gt; ThetaType) -&gt; [ThetaType] -&gt; [ThetaType]
forall a b. (a -&gt; b) -&gt; [a] -&gt; [b]
</span><a href="../../base/src/GHC.Base.html#map"><span class="hs-identifier hs-var">map</span></a></span><span> </span><span class="hs-special">(</span><span class="annot"><span class="annottext">(PredType -&gt; PredType -&gt; Ordering) -&gt; ThetaType -&gt; ThetaType
forall a. (a -&gt; a -&gt; Ordering) -&gt; [a] -&gt; [a]
</span><a href="../../base/src/Data.OldList.html#sortBy"><span class="hs-identifier hs-var">sortBy</span></a></span><span> </span><span class="annot"><span class="annottext">PredType -&gt; PredType -&gt; Ordering
</span><a href="GHC.Core.Type.html#nonDetCmpType"><span class="hs-identifier hs-var">nonDetCmpType</span></a></span><span class="hs-special">)</span><span>
</span><span id="line-688"></span><span>    </span><span class="hs-comment">------------------------------------------------------------------</span><span>
</span><span id="line-689"></span><span>    </span><span class="annot"><a href="#local-6989586621681174244"><span class="hs-identifier hs-type">gen_soln</span></a></span><span> </span><span class="hs-glyph">::</span><span> </span><span class="annot"><a href="GHC.Tc.Deriv.Utils.html#DerivSpec"><span class="hs-identifier hs-type">DerivSpec</span></a></span><span> </span><span class="hs-special">[</span><span class="annot"><a href="GHC.Tc.Deriv.Utils.html#ThetaOrigin"><span class="hs-identifier hs-type">ThetaOrigin</span></a></span><span class="hs-special">]</span><span> </span><span class="hs-glyph">-&gt;</span><span> </span><span class="annot"><a href="GHC.Tc.Types.html#TcM"><span class="hs-identifier hs-type">TcM</span></a></span><span> </span><span class="annot"><a href="GHC.Core.TyCo.Rep.html#ThetaType"><span class="hs-identifier hs-type">ThetaType</span></a></span><span>
</span><span id="line-690"></span><span>    </span><span id="local-6989586621681174244"><span class="annot"><span class="annottext">gen_soln :: DerivSpec [ThetaOrigin] -&gt; IOEnv (Env TcGblEnv TcLclEnv) ThetaType
</span><a href="#local-6989586621681174244"><span class="hs-identifier hs-var hs-var">gen_soln</span></a></span></span><span> </span><span class="hs-special">(</span><span class="annot"><a href="GHC.Tc.Deriv.Utils.html#DS"><span class="hs-identifier hs-type">DS</span></a></span><span> </span><span class="hs-special">{</span><span> </span><span class="annot"><span class="annottext">ds_loc :: forall theta. DerivSpec theta -&gt; SrcSpan
</span><a href="GHC.Tc.Deriv.Utils.html#ds_loc"><span class="hs-identifier hs-var">ds_loc</span></a></span><span> </span><span class="hs-glyph">=</span><span> </span><span id="local-6989586621681174230"><span class="annot"><span class="annottext">SrcSpan
</span><a href="#local-6989586621681174230"><span class="hs-identifier hs-var">loc</span></a></span></span><span class="hs-special">,</span><span> </span><span class="annot"><span class="annottext">ds_tvs :: forall theta. DerivSpec theta -&gt; [TcTyVar]
</span><a href="GHC.Tc.Deriv.Utils.html#ds_tvs"><span class="hs-identifier hs-var">ds_tvs</span></a></span><span> </span><span class="hs-glyph">=</span><span> </span><span id="local-6989586621681174228"><span class="annot"><span class="annottext">[TcTyVar]
</span><a href="#local-6989586621681174228"><span class="hs-identifier hs-var">tyvars</span></a></span></span><span>
</span><span id="line-691"></span><span>                 </span><span class="hs-special">,</span><span> </span><span class="annot"><span class="annottext">ds_cls :: forall theta. DerivSpec theta -&gt; Class
</span><a href="GHC.Tc.Deriv.Utils.html#ds_cls"><span class="hs-identifier hs-var">ds_cls</span></a></span><span> </span><span class="hs-glyph">=</span><span> </span><span id="local-6989586621681174226"><span class="annot"><span class="annottext">Class
</span><a href="#local-6989586621681174226"><span class="hs-identifier hs-var">clas</span></a></span></span><span class="hs-special">,</span><span> </span><span class="annot"><span class="annottext">ds_tys :: forall theta. DerivSpec theta -&gt; ThetaType
</span><a href="GHC.Tc.Deriv.Utils.html#ds_tys"><span class="hs-identifier hs-var">ds_tys</span></a></span><span> </span><span class="hs-glyph">=</span><span> </span><span id="local-6989586621681174224"><span class="annot"><span class="annottext">ThetaType
</span><a href="#local-6989586621681174224"><span class="hs-identifier hs-var">inst_tys</span></a></span></span><span class="hs-special">,</span><span> </span><span class="annot"><span class="annottext">ds_theta :: forall theta. DerivSpec theta -&gt; theta
</span><a href="GHC.Tc.Deriv.Utils.html#ds_theta"><span class="hs-identifier hs-var">ds_theta</span></a></span><span> </span><span class="hs-glyph">=</span><span> </span><span id="local-6989586621681174223"><span class="annot"><span class="annottext">[ThetaOrigin]
</span><a href="#local-6989586621681174223"><span class="hs-identifier hs-var">deriv_rhs</span></a></span></span><span> </span><span class="hs-special">}</span><span class="hs-special">)</span><span>
</span><span id="line-692"></span><span>      </span><span class="hs-glyph">=</span><span> </span><span class="annot"><span class="annottext">SrcSpan
-&gt; IOEnv (Env TcGblEnv TcLclEnv) ThetaType
-&gt; IOEnv (Env TcGblEnv TcLclEnv) ThetaType
forall a. SrcSpan -&gt; TcRn a -&gt; TcRn a
</span><a href="GHC.Tc.Utils.Monad.html#setSrcSpan"><span class="hs-identifier hs-var">setSrcSpan</span></a></span><span> </span><span class="annot"><span class="annottext">SrcSpan
</span><a href="#local-6989586621681174230"><span class="hs-identifier hs-var">loc</span></a></span><span>  </span><span class="annot"><span class="annottext">(IOEnv (Env TcGblEnv TcLclEnv) ThetaType
 -&gt; IOEnv (Env TcGblEnv TcLclEnv) ThetaType)
-&gt; IOEnv (Env TcGblEnv TcLclEnv) ThetaType
-&gt; IOEnv (Env TcGblEnv TcLclEnv) ThetaType
forall a b. (a -&gt; b) -&gt; a -&gt; b
</span><a href="../../base/src/GHC.Base.html#%24"><span class="hs-operator hs-var">$</span></a></span><span>
</span><span id="line-693"></span><span>        </span><span class="annot"><span class="annottext">SDoc
-&gt; IOEnv (Env TcGblEnv TcLclEnv) ThetaType
-&gt; IOEnv (Env TcGblEnv TcLclEnv) ThetaType
forall a. SDoc -&gt; TcM a -&gt; TcM a
</span><a href="GHC.Tc.Utils.Monad.html#addErrCtxt"><span class="hs-identifier hs-var">addErrCtxt</span></a></span><span> </span><span class="hs-special">(</span><span class="annot"><span class="annottext">PredType -&gt; SDoc
</span><a href="GHC.Tc.Deriv.Infer.html#derivInstCtxt"><span class="hs-identifier hs-var">derivInstCtxt</span></a></span><span> </span><span class="annot"><span class="annottext">PredType
</span><a href="#local-6989586621681174219"><span class="hs-identifier hs-var">the_pred</span></a></span><span class="hs-special">)</span><span> </span><span class="annot"><span class="annottext">(IOEnv (Env TcGblEnv TcLclEnv) ThetaType
 -&gt; IOEnv (Env TcGblEnv TcLclEnv) ThetaType)
-&gt; IOEnv (Env TcGblEnv TcLclEnv) ThetaType
-&gt; IOEnv (Env TcGblEnv TcLclEnv) ThetaType
forall a b. (a -&gt; b) -&gt; a -&gt; b
</span><a href="../../base/src/GHC.Base.html#%24"><span class="hs-operator hs-var">$</span></a></span><span>
</span><span id="line-694"></span><span>        </span><span class="hs-keyword">do</span><span> </span><span class="hs-special">{</span><span> </span><span id="local-6989586621681174218"><span class="annot"><span class="annottext">ThetaType
</span><a href="#local-6989586621681174218"><span class="hs-identifier hs-var">theta</span></a></span></span><span> </span><span class="hs-glyph">&lt;-</span><span> </span><span class="annot"><span class="annottext">PredType
-&gt; [TcTyVar]
-&gt; [ThetaOrigin]
-&gt; IOEnv (Env TcGblEnv TcLclEnv) ThetaType
</span><a href="GHC.Tc.Deriv.Infer.html#simplifyDeriv"><span class="hs-identifier hs-var">simplifyDeriv</span></a></span><span> </span><span class="annot"><span class="annottext">PredType
</span><a href="#local-6989586621681174219"><span class="hs-identifier hs-var">the_pred</span></a></span><span> </span><span class="annot"><span class="annottext">[TcTyVar]
</span><a href="#local-6989586621681174228"><span class="hs-identifier hs-var">tyvars</span></a></span><span> </span><span class="annot"><span class="annottext">[ThetaOrigin]
</span><a href="#local-6989586621681174223"><span class="hs-identifier hs-var">deriv_rhs</span></a></span><span>
</span><span id="line-695"></span><span>                </span><span class="hs-comment">-- checkValidInstance tyvars theta clas inst_tys</span><span>
</span><span id="line-696"></span><span>                </span><span class="hs-comment">-- Not necessary; see Note [Exotic derived instance contexts]</span><span>
</span><span id="line-697"></span><span>
</span><span id="line-698"></span><span>           </span><span class="hs-special">;</span><span> </span><span class="annot"><span class="annottext">String -&gt; SDoc -&gt; IOEnv (Env TcGblEnv TcLclEnv) ()
</span><a href="GHC.Tc.Utils.Monad.html#traceTc"><span class="hs-identifier hs-var">traceTc</span></a></span><span> </span><span class="annot"><span class="annottext">String
</span><span class="hs-string">&quot;GHC.Tc.Deriv&quot;</span></span><span> </span><span class="hs-special">(</span><span class="annot"><span class="annottext">[ThetaOrigin] -&gt; SDoc
forall a. Outputable a =&gt; a -&gt; SDoc
</span><a href="GHC.Utils.Outputable.html#ppr"><span class="hs-identifier hs-var">ppr</span></a></span><span> </span><span class="annot"><span class="annottext">[ThetaOrigin]
</span><a href="#local-6989586621681174223"><span class="hs-identifier hs-var">deriv_rhs</span></a></span><span> </span><span class="annot"><span class="annottext">SDoc -&gt; SDoc -&gt; SDoc
</span><a href="GHC.Utils.Outputable.html#%24%24"><span class="hs-operator hs-var">$$</span></a></span><span> </span><span class="annot"><span class="annottext">ThetaType -&gt; SDoc
forall a. Outputable a =&gt; a -&gt; SDoc
</span><a href="GHC.Utils.Outputable.html#ppr"><span class="hs-identifier hs-var">ppr</span></a></span><span> </span><span class="annot"><span class="annottext">ThetaType
</span><a href="#local-6989586621681174218"><span class="hs-identifier hs-var">theta</span></a></span><span class="hs-special">)</span><span>
</span><span id="line-699"></span><span>                </span><span class="hs-comment">-- Claim: the result instance declaration is guaranteed valid</span><span>
</span><span id="line-700"></span><span>                </span><span class="hs-comment">-- Hence no need to call:</span><span>
</span><span id="line-701"></span><span>                </span><span class="hs-comment">--   checkValidInstance tyvars theta clas inst_tys</span><span>
</span><span id="line-702"></span><span>           </span><span class="hs-special">;</span><span> </span><span class="annot"><span class="annottext">ThetaType -&gt; IOEnv (Env TcGblEnv TcLclEnv) ThetaType
forall (m :: * -&gt; *) a. Monad m =&gt; a -&gt; m a
</span><a href="../../base/src/GHC.Base.html#return"><span class="hs-identifier hs-var">return</span></a></span><span> </span><span class="annot"><span class="annottext">ThetaType
</span><a href="#local-6989586621681174218"><span class="hs-identifier hs-var">theta</span></a></span><span> </span><span class="hs-special">}</span><span>
</span><span id="line-703"></span><span>      </span><span class="hs-keyword">where</span><span>
</span><span id="line-704"></span><span>        </span><span id="local-6989586621681174219"><span class="annot"><span class="annottext">the_pred :: PredType
</span><a href="#local-6989586621681174219"><span class="hs-identifier hs-var hs-var">the_pred</span></a></span></span><span> </span><span class="hs-glyph">=</span><span> </span><span class="annot"><span class="annottext">Class -&gt; ThetaType -&gt; PredType
</span><a href="GHC.Core.Predicate.html#mkClassPred"><span class="hs-identifier hs-var">mkClassPred</span></a></span><span> </span><span class="annot"><span class="annottext">Class
</span><a href="#local-6989586621681174226"><span class="hs-identifier hs-var">clas</span></a></span><span> </span><span class="annot"><span class="annottext">ThetaType
</span><a href="#local-6989586621681174224"><span class="hs-identifier hs-var">inst_tys</span></a></span><span>
</span><span id="line-705"></span><span>
</span><span id="line-706"></span><span class="annot"><a href="GHC.Tc.Deriv.Infer.html#derivInstCtxt"><span class="hs-identifier hs-type">derivInstCtxt</span></a></span><span> </span><span class="hs-glyph">::</span><span> </span><span class="annot"><a href="GHC.Core.TyCo.Rep.html#PredType"><span class="hs-identifier hs-type">PredType</span></a></span><span> </span><span class="hs-glyph">-&gt;</span><span> </span><span class="annot"><a href="GHC.Utils.Error.html#MsgDoc"><span class="hs-identifier hs-type">MsgDoc</span></a></span><span>
</span><span id="line-707"></span><span id="derivInstCtxt"><span class="annot"><span class="annottext">derivInstCtxt :: PredType -&gt; SDoc
</span><a href="GHC.Tc.Deriv.Infer.html#derivInstCtxt"><span class="hs-identifier hs-var hs-var">derivInstCtxt</span></a></span></span><span> </span><span id="local-6989586621681174215"><span class="annot"><span class="annottext">PredType
</span><a href="#local-6989586621681174215"><span class="hs-identifier hs-var">pred</span></a></span></span><span>
</span><span id="line-708"></span><span>  </span><span class="hs-glyph">=</span><span> </span><span class="annot"><span class="annottext">String -&gt; SDoc
</span><a href="GHC.Utils.Outputable.html#text"><span class="hs-identifier hs-var">text</span></a></span><span> </span><span class="annot"><span class="annottext">String
</span><span class="hs-string">&quot;When deriving the instance for&quot;</span></span><span> </span><span class="annot"><span class="annottext">SDoc -&gt; SDoc -&gt; SDoc
</span><a href="GHC.Utils.Outputable.html#%3C%2B%3E"><span class="hs-operator hs-var">&lt;+&gt;</span></a></span><span> </span><span class="annot"><span class="annottext">SDoc -&gt; SDoc
</span><a href="GHC.Utils.Outputable.html#parens"><span class="hs-identifier hs-var">parens</span></a></span><span> </span><span class="hs-special">(</span><span class="annot"><span class="annottext">PredType -&gt; SDoc
forall a. Outputable a =&gt; a -&gt; SDoc
</span><a href="GHC.Utils.Outputable.html#ppr"><span class="hs-identifier hs-var">ppr</span></a></span><span> </span><span class="annot"><span class="annottext">PredType
</span><a href="#local-6989586621681174215"><span class="hs-identifier hs-var">pred</span></a></span><span class="hs-special">)</span><span>
</span><span id="line-709"></span><span>
</span><span id="line-710"></span><span class="hs-comment">{-
***********************************************************************************
*                                                                                 *
*            Simplify derived constraints
*                                                                                 *
***********************************************************************************
-}</span><span>
</span><span id="line-717"></span><span>
</span><span id="line-718"></span><span class="hs-comment">-- | Given @instance (wanted) =&gt; C inst_ty@, simplify 'wanted' as much</span><span>
</span><span id="line-719"></span><span class="hs-comment">-- as possible. Fail if not possible.</span><span>
</span><span id="line-720"></span><span class="annot"><a href="GHC.Tc.Deriv.Infer.html#simplifyDeriv"><span class="hs-identifier hs-type">simplifyDeriv</span></a></span><span> </span><span class="hs-glyph">::</span><span> </span><span class="annot"><a href="GHC.Core.TyCo.Rep.html#PredType"><span class="hs-identifier hs-type">PredType</span></a></span><span> </span><span class="hs-comment">-- ^ @C inst_ty@, head of the instance we are</span><span>
</span><span id="line-721"></span><span>                          </span><span class="hs-comment">-- deriving.  Only used for SkolemInfo.</span><span>
</span><span id="line-722"></span><span>              </span><span class="hs-glyph">-&gt;</span><span> </span><span class="hs-special">[</span><span class="annot"><a href="GHC.Types.Var.html#TyVar"><span class="hs-identifier hs-type">TyVar</span></a></span><span class="hs-special">]</span><span>  </span><span class="hs-comment">-- ^ The tyvars bound by @inst_ty@.</span><span>
</span><span id="line-723"></span><span>              </span><span class="hs-glyph">-&gt;</span><span> </span><span class="hs-special">[</span><span class="annot"><a href="GHC.Tc.Deriv.Utils.html#ThetaOrigin"><span class="hs-identifier hs-type">ThetaOrigin</span></a></span><span class="hs-special">]</span><span> </span><span class="hs-comment">-- ^ Given and wanted constraints</span><span>
</span><span id="line-724"></span><span>              </span><span class="hs-glyph">-&gt;</span><span> </span><span class="annot"><a href="GHC.Tc.Types.html#TcM"><span class="hs-identifier hs-type">TcM</span></a></span><span> </span><span class="annot"><a href="GHC.Core.TyCo.Rep.html#ThetaType"><span class="hs-identifier hs-type">ThetaType</span></a></span><span> </span><span class="hs-comment">-- ^ Needed constraints (after simplification),</span><span>
</span><span id="line-725"></span><span>                               </span><span class="hs-comment">-- i.e. @['PredType']@.</span><span>
</span><span id="line-726"></span><span id="simplifyDeriv"><span class="annot"><span class="annottext">simplifyDeriv :: PredType
-&gt; [TcTyVar]
-&gt; [ThetaOrigin]
-&gt; IOEnv (Env TcGblEnv TcLclEnv) ThetaType
</span><a href="GHC.Tc.Deriv.Infer.html#simplifyDeriv"><span class="hs-identifier hs-var hs-var">simplifyDeriv</span></a></span></span><span> </span><span id="local-6989586621681174212"><span class="annot"><span class="annottext">PredType
</span><a href="#local-6989586621681174212"><span class="hs-identifier hs-var">pred</span></a></span></span><span> </span><span id="local-6989586621681174211"><span class="annot"><span class="annottext">[TcTyVar]
</span><a href="#local-6989586621681174211"><span class="hs-identifier hs-var">tvs</span></a></span></span><span> </span><span id="local-6989586621681174210"><span class="annot"><span class="annottext">[ThetaOrigin]
</span><a href="#local-6989586621681174210"><span class="hs-identifier hs-var">thetas</span></a></span></span><span>
</span><span id="line-727"></span><span>  </span><span class="hs-glyph">=</span><span> </span><span class="hs-keyword">do</span><span> </span><span class="hs-special">{</span><span> </span><span class="hs-special">(</span><span id="local-6989586621681174209"><span class="annot"><span class="annottext">TCvSubst
</span><a href="#local-6989586621681174209"><span class="hs-identifier hs-var">skol_subst</span></a></span></span><span class="hs-special">,</span><span> </span><span id="local-6989586621681174208"><span class="annot"><span class="annottext">[TcTyVar]
</span><a href="#local-6989586621681174208"><span class="hs-identifier hs-var">tvs_skols</span></a></span></span><span class="hs-special">)</span><span> </span><span class="hs-glyph">&lt;-</span><span> </span><span class="annot"><span class="annottext">[TcTyVar] -&gt; TcM (TCvSubst, [TcTyVar])
</span><a href="GHC.Tc.Utils.Instantiate.html#tcInstSkolTyVars"><span class="hs-identifier hs-var">tcInstSkolTyVars</span></a></span><span> </span><span class="annot"><span class="annottext">[TcTyVar]
</span><a href="#local-6989586621681174211"><span class="hs-identifier hs-var">tvs</span></a></span><span> </span><span class="hs-comment">-- Skolemize</span><span>
</span><span id="line-728"></span><span>                </span><span class="hs-comment">-- The constraint solving machinery</span><span>
</span><span id="line-729"></span><span>                </span><span class="hs-comment">-- expects *TcTyVars* not TyVars.</span><span>
</span><span id="line-730"></span><span>                </span><span class="hs-comment">-- We use *non-overlappable* (vanilla) skolems</span><span>
</span><span id="line-731"></span><span>                </span><span class="hs-comment">-- See Note [Overlap and deriving]</span><span>
</span><span id="line-732"></span><span>
</span><span id="line-733"></span><span>       </span><span class="hs-special">;</span><span> </span><span class="hs-keyword">let</span><span> </span><span id="local-6989586621681174206"><span class="annot"><span class="annottext">skol_set :: VarSet
</span><a href="#local-6989586621681174206"><span class="hs-identifier hs-var hs-var">skol_set</span></a></span></span><span>  </span><span class="hs-glyph">=</span><span> </span><span class="annot"><span class="annottext">[TcTyVar] -&gt; VarSet
</span><a href="GHC.Types.Var.Set.html#mkVarSet"><span class="hs-identifier hs-var">mkVarSet</span></a></span><span> </span><span class="annot"><span class="annottext">[TcTyVar]
</span><a href="#local-6989586621681174208"><span class="hs-identifier hs-var">tvs_skols</span></a></span><span>
</span><span id="line-734"></span><span>             </span><span id="local-6989586621681174204"><span class="annot"><span class="annottext">skol_info :: SkolemInfo
</span><a href="#local-6989586621681174204"><span class="hs-identifier hs-var hs-var">skol_info</span></a></span></span><span> </span><span class="hs-glyph">=</span><span> </span><span class="annot"><span class="annottext">PredType -&gt; SkolemInfo
</span><a href="GHC.Tc.Types.Origin.html#DerivSkol"><span class="hs-identifier hs-var">DerivSkol</span></a></span><span> </span><span class="annot"><span class="annottext">PredType
</span><a href="#local-6989586621681174212"><span class="hs-identifier hs-var">pred</span></a></span><span>
</span><span id="line-735"></span><span>             </span><span id="local-6989586621681174201"><span class="annot"><span class="annottext">doc :: SDoc
</span><a href="#local-6989586621681174201"><span class="hs-identifier hs-var hs-var">doc</span></a></span></span><span> </span><span class="hs-glyph">=</span><span> </span><span class="annot"><span class="annottext">String -&gt; SDoc
</span><a href="GHC.Utils.Outputable.html#text"><span class="hs-identifier hs-var">text</span></a></span><span> </span><span class="annot"><span class="annottext">String
</span><span class="hs-string">&quot;deriving&quot;</span></span><span> </span><span class="annot"><span class="annottext">SDoc -&gt; SDoc -&gt; SDoc
</span><a href="GHC.Utils.Outputable.html#%3C%2B%3E"><span class="hs-operator hs-var">&lt;+&gt;</span></a></span><span> </span><span class="annot"><span class="annottext">SDoc -&gt; SDoc
</span><a href="GHC.Utils.Outputable.html#parens"><span class="hs-identifier hs-var">parens</span></a></span><span> </span><span class="hs-special">(</span><span class="annot"><span class="annottext">PredType -&gt; SDoc
forall a. Outputable a =&gt; a -&gt; SDoc
</span><a href="GHC.Utils.Outputable.html#ppr"><span class="hs-identifier hs-var">ppr</span></a></span><span> </span><span class="annot"><span class="annottext">PredType
</span><a href="#local-6989586621681174212"><span class="hs-identifier hs-var">pred</span></a></span><span class="hs-special">)</span><span>
</span><span id="line-736"></span><span>
</span><span id="line-737"></span><span>             </span><span class="annot"><a href="#local-6989586621681174200"><span class="hs-identifier hs-type">mk_given_ev</span></a></span><span> </span><span class="hs-glyph">::</span><span> </span><span class="annot"><a href="GHC.Core.TyCo.Rep.html#PredType"><span class="hs-identifier hs-type">PredType</span></a></span><span> </span><span class="hs-glyph">-&gt;</span><span> </span><span class="annot"><a href="GHC.Tc.Types.html#TcM"><span class="hs-identifier hs-type">TcM</span></a></span><span> </span><span class="annot"><a href="GHC.Types.Var.html#EvVar"><span class="hs-identifier hs-type">EvVar</span></a></span><span>
</span><span id="line-738"></span><span>             </span><span id="local-6989586621681174200"><span class="annot"><span class="annottext">mk_given_ev :: PredType -&gt; TcM TcTyVar
</span><a href="#local-6989586621681174200"><span class="hs-identifier hs-var hs-var">mk_given_ev</span></a></span></span><span> </span><span id="local-6989586621681174198"><span class="annot"><span class="annottext">PredType
</span><a href="#local-6989586621681174198"><span class="hs-identifier hs-var">given</span></a></span></span><span> </span><span class="hs-glyph">=</span><span>
</span><span id="line-739"></span><span>               </span><span class="hs-keyword">let</span><span> </span><span id="local-6989586621681174195"><span class="annot"><span class="annottext">given_pred :: PredType
</span><a href="#local-6989586621681174195"><span class="hs-identifier hs-var hs-var">given_pred</span></a></span></span><span> </span><span class="hs-glyph">=</span><span> </span><span class="annot"><span class="annottext">HasCallStack =&gt; TCvSubst -&gt; PredType -&gt; PredType
TCvSubst -&gt; PredType -&gt; PredType
</span><a href="GHC.Core.TyCo.Subst.html#substTy"><span class="hs-identifier hs-var">substTy</span></a></span><span> </span><span class="annot"><span class="annottext">TCvSubst
</span><a href="#local-6989586621681174209"><span class="hs-identifier hs-var">skol_subst</span></a></span><span> </span><span class="annot"><span class="annottext">PredType
</span><a href="#local-6989586621681174198"><span class="hs-identifier hs-var">given</span></a></span><span>
</span><span id="line-740"></span><span>               </span><span class="hs-keyword">in</span><span> </span><span class="annot"><span class="annottext">PredType -&gt; TcM TcTyVar
forall gbl lcl. PredType -&gt; TcRnIf gbl lcl TcTyVar
</span><a href="GHC.Tc.Utils.TcMType.html#newEvVar"><span class="hs-identifier hs-var">newEvVar</span></a></span><span> </span><span class="annot"><span class="annottext">PredType
</span><a href="#local-6989586621681174195"><span class="hs-identifier hs-var">given_pred</span></a></span><span>
</span><span id="line-741"></span><span>
</span><span id="line-742"></span><span>             </span><span class="annot"><a href="#local-6989586621681174192"><span class="hs-identifier hs-type">emit_wanted_constraints</span></a></span><span> </span><span class="hs-glyph">::</span><span> </span><span class="hs-special">[</span><span class="annot"><a href="GHC.Types.Var.html#TyVar"><span class="hs-identifier hs-type">TyVar</span></a></span><span class="hs-special">]</span><span> </span><span class="hs-glyph">-&gt;</span><span> </span><span class="hs-special">[</span><span class="annot"><a href="GHC.Tc.Deriv.Utils.html#PredOrigin"><span class="hs-identifier hs-type">PredOrigin</span></a></span><span class="hs-special">]</span><span> </span><span class="hs-glyph">-&gt;</span><span> </span><span class="annot"><a href="GHC.Tc.Types.html#TcM"><span class="hs-identifier hs-type">TcM</span></a></span><span> </span><span class="hs-special">(</span><span class="hs-special">)</span><span>
</span><span id="line-743"></span><span>             </span><span id="local-6989586621681174192"><span class="annot"><span class="annottext">emit_wanted_constraints :: [TcTyVar] -&gt; [PredOrigin] -&gt; IOEnv (Env TcGblEnv TcLclEnv) ()
</span><a href="#local-6989586621681174192"><span class="hs-identifier hs-var hs-var">emit_wanted_constraints</span></a></span></span><span> </span><span id="local-6989586621681174191"><span class="annot"><span class="annottext">[TcTyVar]
</span><a href="#local-6989586621681174191"><span class="hs-identifier hs-var">metas_to_be</span></a></span></span><span> </span><span id="local-6989586621681174190"><span class="annot"><span class="annottext">[PredOrigin]
</span><a href="#local-6989586621681174190"><span class="hs-identifier hs-var">preds</span></a></span></span><span>
</span><span id="line-744"></span><span>               </span><span class="hs-glyph">=</span><span> </span><span class="hs-keyword">do</span><span> </span><span class="hs-special">{</span><span> </span><span class="hs-comment">-- We instantiate metas_to_be with fresh meta type</span><span>
</span><span id="line-745"></span><span>                      </span><span class="hs-comment">-- variables. Currently, these can only be type variables</span><span>
</span><span id="line-746"></span><span>                      </span><span class="hs-comment">-- quantified in generic default type signatures.</span><span>
</span><span id="line-747"></span><span>                      </span><span class="hs-comment">-- See Note [Gathering and simplifying constraints for</span><span>
</span><span id="line-748"></span><span>                      </span><span class="hs-comment">-- DeriveAnyClass]</span><span>
</span><span id="line-749"></span><span>                      </span><span class="hs-special">(</span><span id="local-6989586621681174189"><span class="annot"><span class="annottext">TCvSubst
</span><a href="#local-6989586621681174189"><span class="hs-identifier hs-var">meta_subst</span></a></span></span><span class="hs-special">,</span><span> </span><span id="local-6989586621681174188"><span class="annot"><span class="annottext">[TcTyVar]
</span><a href="#local-6989586621681174188"><span class="hs-identifier hs-var">_meta_tvs</span></a></span></span><span class="hs-special">)</span><span> </span><span class="hs-glyph">&lt;-</span><span> </span><span class="annot"><span class="annottext">[TcTyVar] -&gt; TcM (TCvSubst, [TcTyVar])
</span><a href="GHC.Tc.Utils.TcMType.html#newMetaTyVars"><span class="hs-identifier hs-var">newMetaTyVars</span></a></span><span> </span><span class="annot"><span class="annottext">[TcTyVar]
</span><a href="#local-6989586621681174191"><span class="hs-identifier hs-var">metas_to_be</span></a></span><span>
</span><span id="line-750"></span><span>
</span><span id="line-751"></span><span>                    </span><span class="hs-comment">-- Now make a constraint for each of the instantiated predicates</span><span>
</span><span id="line-752"></span><span>                    </span><span class="hs-special">;</span><span> </span><span class="hs-keyword">let</span><span> </span><span id="local-6989586621681174186"><span class="annot"><span class="annottext">wanted_subst :: TCvSubst
</span><a href="#local-6989586621681174186"><span class="hs-identifier hs-var hs-var">wanted_subst</span></a></span></span><span> </span><span class="hs-glyph">=</span><span> </span><span class="annot"><span class="annottext">TCvSubst
</span><a href="#local-6989586621681174209"><span class="hs-identifier hs-var">skol_subst</span></a></span><span> </span><span class="annot"><span class="annottext">TCvSubst -&gt; TCvSubst -&gt; TCvSubst
</span><a href="GHC.Core.TyCo.Subst.html#unionTCvSubst"><span class="hs-operator hs-var">`unionTCvSubst`</span></a></span><span> </span><span class="annot"><span class="annottext">TCvSubst
</span><a href="#local-6989586621681174189"><span class="hs-identifier hs-var">meta_subst</span></a></span><span>
</span><span id="line-753"></span><span>                          </span><span id="local-6989586621681174182"><span class="annot"><span class="annottext">mk_wanted_ct :: PredOrigin -&gt; IOEnv (Env TcGblEnv TcLclEnv) Ct
</span><a href="#local-6989586621681174182"><span class="hs-identifier hs-var hs-var">mk_wanted_ct</span></a></span></span><span> </span><span class="hs-special">(</span><span class="annot"><a href="GHC.Tc.Deriv.Utils.html#PredOrigin"><span class="hs-identifier hs-type">PredOrigin</span></a></span><span> </span><span id="local-6989586621681174180"><span class="annot"><span class="annottext">PredType
</span><a href="#local-6989586621681174180"><span class="hs-identifier hs-var">wanted</span></a></span></span><span> </span><span id="local-6989586621681174179"><span class="annot"><span class="annottext">CtOrigin
</span><a href="#local-6989586621681174179"><span class="hs-identifier hs-var">orig</span></a></span></span><span> </span><span id="local-6989586621681174178"><span class="annot"><span class="annottext">TypeOrKind
</span><a href="#local-6989586621681174178"><span class="hs-identifier hs-var">t_or_k</span></a></span></span><span class="hs-special">)</span><span>
</span><span id="line-754"></span><span>                            </span><span class="hs-glyph">=</span><span> </span><span class="hs-keyword">do</span><span> </span><span class="hs-special">{</span><span> </span><span id="local-6989586621681174177"><span class="annot"><span class="annottext">CtEvidence
</span><a href="#local-6989586621681174177"><span class="hs-identifier hs-var">ev</span></a></span></span><span> </span><span class="hs-glyph">&lt;-</span><span> </span><span class="annot"><span class="annottext">CtOrigin -&gt; Maybe TypeOrKind -&gt; PredType -&gt; TcM CtEvidence
</span><a href="GHC.Tc.Utils.TcMType.html#newWanted"><span class="hs-identifier hs-var">newWanted</span></a></span><span> </span><span class="annot"><span class="annottext">CtOrigin
</span><a href="#local-6989586621681174179"><span class="hs-identifier hs-var">orig</span></a></span><span> </span><span class="hs-special">(</span><span class="annot"><span class="annottext">TypeOrKind -&gt; Maybe TypeOrKind
forall a. a -&gt; Maybe a
</span><a href="../../base/src/GHC.Maybe.html#Just"><span class="hs-identifier hs-var">Just</span></a></span><span> </span><span class="annot"><span class="annottext">TypeOrKind
</span><a href="#local-6989586621681174178"><span class="hs-identifier hs-var">t_or_k</span></a></span><span class="hs-special">)</span><span> </span><span class="annot"><span class="annottext">(PredType -&gt; TcM CtEvidence) -&gt; PredType -&gt; TcM CtEvidence
forall a b. (a -&gt; b) -&gt; a -&gt; b
</span><a href="../../base/src/GHC.Base.html#%24"><span class="hs-operator hs-var">$</span></a></span><span>
</span><span id="line-755"></span><span>                                         </span><span class="annot"><span class="annottext">TCvSubst -&gt; PredType -&gt; PredType
</span><a href="GHC.Core.TyCo.Subst.html#substTyUnchecked"><span class="hs-identifier hs-var">substTyUnchecked</span></a></span><span> </span><span class="annot"><span class="annottext">TCvSubst
</span><a href="#local-6989586621681174186"><span class="hs-identifier hs-var">wanted_subst</span></a></span><span> </span><span class="annot"><span class="annottext">PredType
</span><a href="#local-6989586621681174180"><span class="hs-identifier hs-var">wanted</span></a></span><span>
</span><span id="line-756"></span><span>                                 </span><span class="hs-special">;</span><span> </span><span class="annot"><span class="annottext">Ct -&gt; IOEnv (Env TcGblEnv TcLclEnv) Ct
forall (m :: * -&gt; *) a. Monad m =&gt; a -&gt; m a
</span><a href="../../base/src/GHC.Base.html#return"><span class="hs-identifier hs-var">return</span></a></span><span> </span><span class="hs-special">(</span><span class="annot"><span class="annottext">CtEvidence -&gt; Ct
</span><a href="GHC.Tc.Types.Constraint.html#mkNonCanonical"><span class="hs-identifier hs-var">mkNonCanonical</span></a></span><span> </span><span class="annot"><span class="annottext">CtEvidence
</span><a href="#local-6989586621681174177"><span class="hs-identifier hs-var">ev</span></a></span><span class="hs-special">)</span><span> </span><span class="hs-special">}</span><span>
</span><span id="line-757"></span><span>                    </span><span class="hs-special">;</span><span> </span><span id="local-6989586621681174173"><span class="annot"><span class="annottext">[Ct]
</span><a href="#local-6989586621681174173"><span class="hs-identifier hs-var">cts</span></a></span></span><span> </span><span class="hs-glyph">&lt;-</span><span> </span><span class="annot"><span class="annottext">(PredOrigin -&gt; IOEnv (Env TcGblEnv TcLclEnv) Ct)
-&gt; [PredOrigin] -&gt; IOEnv (Env TcGblEnv TcLclEnv) [Ct]
forall (t :: * -&gt; *) (m :: * -&gt; *) a b.
(Traversable t, Monad m) =&gt;
(a -&gt; m b) -&gt; t a -&gt; m (t b)
</span><a href="../../base/src/Data.Traversable.html#mapM"><span class="hs-identifier hs-var">mapM</span></a></span><span> </span><span class="annot"><span class="annottext">PredOrigin -&gt; IOEnv (Env TcGblEnv TcLclEnv) Ct
</span><a href="#local-6989586621681174182"><span class="hs-identifier hs-var">mk_wanted_ct</span></a></span><span> </span><span class="annot"><span class="annottext">[PredOrigin]
</span><a href="#local-6989586621681174190"><span class="hs-identifier hs-var">preds</span></a></span><span>
</span><span id="line-758"></span><span>
</span><span id="line-759"></span><span>                    </span><span class="hs-comment">-- And emit them into the monad</span><span>
</span><span id="line-760"></span><span>                    </span><span class="hs-special">;</span><span> </span><span class="annot"><span class="annottext">Cts -&gt; IOEnv (Env TcGblEnv TcLclEnv) ()
</span><a href="GHC.Tc.Utils.Monad.html#emitSimples"><span class="hs-identifier hs-var">emitSimples</span></a></span><span> </span><span class="hs-special">(</span><span class="annot"><span class="annottext">[Ct] -&gt; Cts
</span><a href="GHC.Tc.Types.Constraint.html#listToCts"><span class="hs-identifier hs-var">listToCts</span></a></span><span> </span><span class="annot"><span class="annottext">[Ct]
</span><a href="#local-6989586621681174173"><span class="hs-identifier hs-var">cts</span></a></span><span class="hs-special">)</span><span> </span><span class="hs-special">}</span><span>
</span><span id="line-761"></span><span>
</span><span id="line-762"></span><span>             </span><span class="hs-comment">-- Create the implications we need to solve. For stock and newtype</span><span>
</span><span id="line-763"></span><span>             </span><span class="hs-comment">-- deriving, these implication constraints will be simple class</span><span>
</span><span id="line-764"></span><span>             </span><span class="hs-comment">-- constraints like (C a, Ord b).</span><span>
</span><span id="line-765"></span><span>             </span><span class="hs-comment">-- But with DeriveAnyClass, we make an implication constraint.</span><span>
</span><span id="line-766"></span><span>             </span><span class="hs-comment">-- See Note [Gathering and simplifying constraints for DeriveAnyClass]</span><span>
</span><span id="line-767"></span><span>             </span><span class="annot"><a href="#local-6989586621681174170"><span class="hs-identifier hs-type">mk_wanteds</span></a></span><span> </span><span class="hs-glyph">::</span><span> </span><span class="annot"><a href="GHC.Tc.Deriv.Utils.html#ThetaOrigin"><span class="hs-identifier hs-type">ThetaOrigin</span></a></span><span> </span><span class="hs-glyph">-&gt;</span><span> </span><span class="annot"><a href="GHC.Tc.Types.html#TcM"><span class="hs-identifier hs-type">TcM</span></a></span><span> </span><span class="annot"><a href="GHC.Tc.Types.Constraint.html#WantedConstraints"><span class="hs-identifier hs-type">WantedConstraints</span></a></span><span>
</span><span id="line-768"></span><span>             </span><span id="local-6989586621681174170"><span class="annot"><span class="annottext">mk_wanteds :: ThetaOrigin -&gt; TcM WantedConstraints
</span><a href="#local-6989586621681174170"><span class="hs-identifier hs-var hs-var">mk_wanteds</span></a></span></span><span> </span><span class="hs-special">(</span><span class="annot"><a href="GHC.Tc.Deriv.Utils.html#ThetaOrigin"><span class="hs-identifier hs-type">ThetaOrigin</span></a></span><span> </span><span class="hs-special">{</span><span> </span><span class="annot"><span class="annottext">to_anyclass_skols :: ThetaOrigin -&gt; [TcTyVar]
</span><a href="GHC.Tc.Deriv.Utils.html#to_anyclass_skols"><span class="hs-identifier hs-var">to_anyclass_skols</span></a></span><span>  </span><span class="hs-glyph">=</span><span> </span><span id="local-6989586621681174167"><span class="annot"><span class="annottext">[TcTyVar]
</span><a href="#local-6989586621681174167"><span class="hs-identifier hs-var">ac_skols</span></a></span></span><span>
</span><span id="line-769"></span><span>                                     </span><span class="hs-special">,</span><span> </span><span class="annot"><span class="annottext">to_anyclass_metas :: ThetaOrigin -&gt; [TcTyVar]
</span><a href="GHC.Tc.Deriv.Utils.html#to_anyclass_metas"><span class="hs-identifier hs-var">to_anyclass_metas</span></a></span><span>  </span><span class="hs-glyph">=</span><span> </span><span id="local-6989586621681174165"><span class="annot"><span class="annottext">[TcTyVar]
</span><a href="#local-6989586621681174165"><span class="hs-identifier hs-var">ac_metas</span></a></span></span><span>
</span><span id="line-770"></span><span>                                     </span><span class="hs-special">,</span><span> </span><span class="annot"><span class="annottext">to_anyclass_givens :: ThetaOrigin -&gt; ThetaType
</span><a href="GHC.Tc.Deriv.Utils.html#to_anyclass_givens"><span class="hs-identifier hs-var">to_anyclass_givens</span></a></span><span> </span><span class="hs-glyph">=</span><span> </span><span id="local-6989586621681174163"><span class="annot"><span class="annottext">ThetaType
</span><a href="#local-6989586621681174163"><span class="hs-identifier hs-var">ac_givens</span></a></span></span><span>
</span><span id="line-771"></span><span>                                     </span><span class="hs-special">,</span><span> </span><span class="annot"><span class="annottext">to_wanted_origins :: ThetaOrigin -&gt; [PredOrigin]
</span><a href="GHC.Tc.Deriv.Utils.html#to_wanted_origins"><span class="hs-identifier hs-var">to_wanted_origins</span></a></span><span>  </span><span class="hs-glyph">=</span><span> </span><span id="local-6989586621681174161"><span class="annot"><span class="annottext">[PredOrigin]
</span><a href="#local-6989586621681174161"><span class="hs-identifier hs-var">preds</span></a></span></span><span> </span><span class="hs-special">}</span><span class="hs-special">)</span><span>
</span><span id="line-772"></span><span>               </span><span class="hs-glyph">=</span><span> </span><span class="hs-keyword">do</span><span> </span><span class="hs-special">{</span><span> </span><span id="local-6989586621681174160"><span class="annot"><span class="annottext">[TcTyVar]
</span><a href="#local-6989586621681174160"><span class="hs-identifier hs-var">ac_given_evs</span></a></span></span><span> </span><span class="hs-glyph">&lt;-</span><span> </span><span class="annot"><span class="annottext">(PredType -&gt; TcM TcTyVar)
-&gt; ThetaType -&gt; IOEnv (Env TcGblEnv TcLclEnv) [TcTyVar]
forall (t :: * -&gt; *) (m :: * -&gt; *) a b.
(Traversable t, Monad m) =&gt;
(a -&gt; m b) -&gt; t a -&gt; m (t b)
</span><a href="../../base/src/Data.Traversable.html#mapM"><span class="hs-identifier hs-var">mapM</span></a></span><span> </span><span class="annot"><span class="annottext">PredType -&gt; TcM TcTyVar
</span><a href="#local-6989586621681174200"><span class="hs-identifier hs-var">mk_given_ev</span></a></span><span> </span><span class="annot"><span class="annottext">ThetaType
</span><a href="#local-6989586621681174163"><span class="hs-identifier hs-var">ac_givens</span></a></span><span>
</span><span id="line-773"></span><span>                    </span><span class="hs-special">;</span><span> </span><span class="hs-special">(</span><span class="annot"><span class="annottext">(TcEvBinds, ())
</span><span class="hs-identifier">_</span></span><span class="hs-special">,</span><span> </span><span id="local-6989586621681174159"><span class="annot"><span class="annottext">WantedConstraints
</span><a href="#local-6989586621681174159"><span class="hs-identifier hs-var">wanteds</span></a></span></span><span class="hs-special">)</span><span>
</span><span id="line-774"></span><span>                        </span><span class="hs-glyph">&lt;-</span><span> </span><span class="annot"><span class="annottext">TcM (TcEvBinds, ()) -&gt; TcM ((TcEvBinds, ()), WantedConstraints)
forall a. TcM a -&gt; TcM (a, WantedConstraints)
</span><a href="GHC.Tc.Utils.Monad.html#captureConstraints"><span class="hs-identifier hs-var">captureConstraints</span></a></span><span> </span><span class="annot"><span class="annottext">(TcM (TcEvBinds, ()) -&gt; TcM ((TcEvBinds, ()), WantedConstraints))
-&gt; TcM (TcEvBinds, ()) -&gt; TcM ((TcEvBinds, ()), WantedConstraints)
forall a b. (a -&gt; b) -&gt; a -&gt; b
</span><a href="../../base/src/GHC.Base.html#%24"><span class="hs-operator hs-var">$</span></a></span><span>
</span><span id="line-775"></span><span>                           </span><span class="annot"><span class="annottext">SkolemInfo
-&gt; [TcTyVar]
-&gt; [TcTyVar]
-&gt; IOEnv (Env TcGblEnv TcLclEnv) ()
-&gt; TcM (TcEvBinds, ())
forall result.
SkolemInfo
-&gt; [TcTyVar] -&gt; [TcTyVar] -&gt; TcM result -&gt; TcM (TcEvBinds, result)
</span><a href="GHC.Tc.Utils.Unify.html#checkConstraints"><span class="hs-identifier hs-var">checkConstraints</span></a></span><span> </span><span class="annot"><span class="annottext">SkolemInfo
</span><a href="#local-6989586621681174204"><span class="hs-identifier hs-var">skol_info</span></a></span><span> </span><span class="annot"><span class="annottext">[TcTyVar]
</span><a href="#local-6989586621681174167"><span class="hs-identifier hs-var">ac_skols</span></a></span><span> </span><span class="annot"><span class="annottext">[TcTyVar]
</span><a href="#local-6989586621681174160"><span class="hs-identifier hs-var">ac_given_evs</span></a></span><span> </span><span class="annot"><span class="annottext">(IOEnv (Env TcGblEnv TcLclEnv) () -&gt; TcM (TcEvBinds, ()))
-&gt; IOEnv (Env TcGblEnv TcLclEnv) () -&gt; TcM (TcEvBinds, ())
forall a b. (a -&gt; b) -&gt; a -&gt; b
</span><a href="../../base/src/GHC.Base.html#%24"><span class="hs-operator hs-var">$</span></a></span><span>
</span><span id="line-776"></span><span>                              </span><span class="hs-comment">-- The checkConstraints bumps the TcLevel, and</span><span>
</span><span id="line-777"></span><span>                              </span><span class="hs-comment">-- wraps the wanted constraints in an implication,</span><span>
</span><span id="line-778"></span><span>                              </span><span class="hs-comment">-- when (but only when) necessary</span><span>
</span><span id="line-779"></span><span>                           </span><span class="annot"><span class="annottext">[TcTyVar] -&gt; [PredOrigin] -&gt; IOEnv (Env TcGblEnv TcLclEnv) ()
</span><a href="#local-6989586621681174192"><span class="hs-identifier hs-var">emit_wanted_constraints</span></a></span><span> </span><span class="annot"><span class="annottext">[TcTyVar]
</span><a href="#local-6989586621681174165"><span class="hs-identifier hs-var">ac_metas</span></a></span><span> </span><span class="annot"><span class="annottext">[PredOrigin]
</span><a href="#local-6989586621681174161"><span class="hs-identifier hs-var">preds</span></a></span><span>
</span><span id="line-780"></span><span>                    </span><span class="hs-special">;</span><span> </span><span class="annot"><span class="annottext">WantedConstraints -&gt; TcM WantedConstraints
forall (f :: * -&gt; *) a. Applicative f =&gt; a -&gt; f a
</span><a href="../../base/src/GHC.Base.html#pure"><span class="hs-identifier hs-var">pure</span></a></span><span> </span><span class="annot"><span class="annottext">WantedConstraints
</span><a href="#local-6989586621681174159"><span class="hs-identifier hs-var">wanteds</span></a></span><span> </span><span class="hs-special">}</span><span>
</span><span id="line-781"></span><span>
</span><span id="line-782"></span><span>       </span><span class="hs-comment">-- See [STEP DAC BUILD]</span><span>
</span><span id="line-783"></span><span>       </span><span class="hs-comment">-- Generate the implication constraints, one for each method, to solve</span><span>
</span><span id="line-784"></span><span>       </span><span class="hs-comment">-- with the skolemized variables.  Start &quot;one level down&quot; because</span><span>
</span><span id="line-785"></span><span>       </span><span class="hs-comment">-- we are going to wrap the result in an implication with tvs_skols,</span><span>
</span><span id="line-786"></span><span>       </span><span class="hs-comment">-- in step [DAC RESIDUAL]</span><span>
</span><span id="line-787"></span><span>       </span><span class="hs-special">;</span><span> </span><span class="hs-special">(</span><span id="local-6989586621681174157"><span class="annot"><span class="annottext">TcLevel
</span><a href="#local-6989586621681174157"><span class="hs-identifier hs-var">tc_lvl</span></a></span></span><span class="hs-special">,</span><span> </span><span id="local-6989586621681174156"><span class="annot"><span class="annottext">[WantedConstraints]
</span><a href="#local-6989586621681174156"><span class="hs-identifier hs-var">wanteds</span></a></span></span><span class="hs-special">)</span><span> </span><span class="hs-glyph">&lt;-</span><span> </span><span class="annot"><span class="annottext">TcM [WantedConstraints] -&gt; TcM (TcLevel, [WantedConstraints])
forall a. TcM a -&gt; TcM (TcLevel, a)
</span><a href="GHC.Tc.Utils.Monad.html#pushTcLevelM"><span class="hs-identifier hs-var">pushTcLevelM</span></a></span><span> </span><span class="annot"><span class="annottext">(TcM [WantedConstraints] -&gt; TcM (TcLevel, [WantedConstraints]))
-&gt; TcM [WantedConstraints] -&gt; TcM (TcLevel, [WantedConstraints])
forall a b. (a -&gt; b) -&gt; a -&gt; b
</span><a href="../../base/src/GHC.Base.html#%24"><span class="hs-operator hs-var">$</span></a></span><span>
</span><span id="line-788"></span><span>                              </span><span class="annot"><span class="annottext">(ThetaOrigin -&gt; TcM WantedConstraints)
-&gt; [ThetaOrigin] -&gt; TcM [WantedConstraints]
forall (t :: * -&gt; *) (m :: * -&gt; *) a b.
(Traversable t, Monad m) =&gt;
(a -&gt; m b) -&gt; t a -&gt; m (t b)
</span><a href="../../base/src/Data.Traversable.html#mapM"><span class="hs-identifier hs-var">mapM</span></a></span><span> </span><span class="annot"><span class="annottext">ThetaOrigin -&gt; TcM WantedConstraints
</span><a href="#local-6989586621681174170"><span class="hs-identifier hs-var">mk_wanteds</span></a></span><span> </span><span class="annot"><span class="annottext">[ThetaOrigin]
</span><a href="#local-6989586621681174210"><span class="hs-identifier hs-var">thetas</span></a></span><span>
</span><span id="line-789"></span><span>
</span><span id="line-790"></span><span>       </span><span class="hs-special">;</span><span> </span><span class="annot"><span class="annottext">String -&gt; SDoc -&gt; IOEnv (Env TcGblEnv TcLclEnv) ()
</span><a href="GHC.Tc.Utils.Monad.html#traceTc"><span class="hs-identifier hs-var">traceTc</span></a></span><span> </span><span class="annot"><span class="annottext">String
</span><span class="hs-string">&quot;simplifyDeriv inputs&quot;</span></span><span> </span><span class="annot"><span class="annottext">(SDoc -&gt; IOEnv (Env TcGblEnv TcLclEnv) ())
-&gt; SDoc -&gt; IOEnv (Env TcGblEnv TcLclEnv) ()
forall a b. (a -&gt; b) -&gt; a -&gt; b
</span><a href="../../base/src/GHC.Base.html#%24"><span class="hs-operator hs-var">$</span></a></span><span>
</span><span id="line-791"></span><span>         </span><span class="annot"><span class="annottext">[SDoc] -&gt; SDoc
</span><a href="GHC.Utils.Outputable.html#vcat"><span class="hs-identifier hs-var">vcat</span></a></span><span> </span><span class="hs-special">[</span><span> </span><span class="annot"><span class="annottext">[TcTyVar] -&gt; SDoc
</span><a href="GHC.Core.TyCo.Ppr.html#pprTyVars"><span class="hs-identifier hs-var">pprTyVars</span></a></span><span> </span><span class="annot"><span class="annottext">[TcTyVar]
</span><a href="#local-6989586621681174211"><span class="hs-identifier hs-var">tvs</span></a></span><span> </span><span class="annot"><span class="annottext">SDoc -&gt; SDoc -&gt; SDoc
</span><a href="GHC.Utils.Outputable.html#%24%24"><span class="hs-operator hs-var">$$</span></a></span><span> </span><span class="annot"><span class="annottext">[ThetaOrigin] -&gt; SDoc
forall a. Outputable a =&gt; a -&gt; SDoc
</span><a href="GHC.Utils.Outputable.html#ppr"><span class="hs-identifier hs-var">ppr</span></a></span><span> </span><span class="annot"><span class="annottext">[ThetaOrigin]
</span><a href="#local-6989586621681174210"><span class="hs-identifier hs-var">thetas</span></a></span><span> </span><span class="annot"><span class="annottext">SDoc -&gt; SDoc -&gt; SDoc
</span><a href="GHC.Utils.Outputable.html#%24%24"><span class="hs-operator hs-var">$$</span></a></span><span> </span><span class="annot"><span class="annottext">[WantedConstraints] -&gt; SDoc
forall a. Outputable a =&gt; a -&gt; SDoc
</span><a href="GHC.Utils.Outputable.html#ppr"><span class="hs-identifier hs-var">ppr</span></a></span><span> </span><span class="annot"><span class="annottext">[WantedConstraints]
</span><a href="#local-6989586621681174156"><span class="hs-identifier hs-var">wanteds</span></a></span><span class="hs-special">,</span><span> </span><span class="annot"><span class="annottext">SDoc
</span><a href="#local-6989586621681174201"><span class="hs-identifier hs-var">doc</span></a></span><span> </span><span class="hs-special">]</span><span>
</span><span id="line-792"></span><span>
</span><span id="line-793"></span><span>       </span><span class="hs-comment">-- See [STEP DAC SOLVE]</span><span>
</span><span id="line-794"></span><span>       </span><span class="hs-comment">-- Simplify the constraints, starting at the same level at which</span><span>
</span><span id="line-795"></span><span>       </span><span class="hs-comment">-- they are generated (c.f. the call to runTcSWithEvBinds in</span><span>
</span><span id="line-796"></span><span>       </span><span class="hs-comment">-- simplifyInfer)</span><span>
</span><span id="line-797"></span><span>       </span><span class="hs-special">;</span><span> </span><span id="local-6989586621681174154"><span class="annot"><span class="annottext">WantedConstraints
</span><a href="#local-6989586621681174154"><span class="hs-identifier hs-var">solved_wanteds</span></a></span></span><span> </span><span class="hs-glyph">&lt;-</span><span> </span><span class="annot"><span class="annottext">TcLevel -&gt; TcM WantedConstraints -&gt; TcM WantedConstraints
forall a. TcLevel -&gt; TcM a -&gt; TcM a
</span><a href="GHC.Tc.Utils.Monad.html#setTcLevel"><span class="hs-identifier hs-var">setTcLevel</span></a></span><span> </span><span class="annot"><span class="annottext">TcLevel
</span><a href="#local-6989586621681174157"><span class="hs-identifier hs-var">tc_lvl</span></a></span><span>   </span><span class="annot"><span class="annottext">(TcM WantedConstraints -&gt; TcM WantedConstraints)
-&gt; TcM WantedConstraints -&gt; TcM WantedConstraints
forall a b. (a -&gt; b) -&gt; a -&gt; b
</span><a href="../../base/src/GHC.Base.html#%24"><span class="hs-operator hs-var">$</span></a></span><span>
</span><span id="line-798"></span><span>                           </span><span class="annot"><span class="annottext">TcS WantedConstraints -&gt; TcM WantedConstraints
forall a. TcS a -&gt; TcM a
</span><a href="GHC.Tc.Solver.Monad.html#runTcSDeriveds"><span class="hs-identifier hs-var">runTcSDeriveds</span></a></span><span>      </span><span class="annot"><span class="annottext">(TcS WantedConstraints -&gt; TcM WantedConstraints)
-&gt; TcS WantedConstraints -&gt; TcM WantedConstraints
forall a b. (a -&gt; b) -&gt; a -&gt; b
</span><a href="../../base/src/GHC.Base.html#%24"><span class="hs-operator hs-var">$</span></a></span><span>
</span><span id="line-799"></span><span>                           </span><span class="annot"><span class="annottext">WantedConstraints -&gt; TcS WantedConstraints
</span><a href="GHC.Tc.Solver.html#solveWantedsAndDrop"><span class="hs-identifier hs-var">solveWantedsAndDrop</span></a></span><span> </span><span class="annot"><span class="annottext">(WantedConstraints -&gt; TcS WantedConstraints)
-&gt; WantedConstraints -&gt; TcS WantedConstraints
forall a b. (a -&gt; b) -&gt; a -&gt; b
</span><a href="../../base/src/GHC.Base.html#%24"><span class="hs-operator hs-var">$</span></a></span><span>
</span><span id="line-800"></span><span>                           </span><span class="annot"><span class="annottext">[WantedConstraints] -&gt; WantedConstraints
</span><a href="GHC.Tc.Types.Constraint.html#unionsWC"><span class="hs-identifier hs-var">unionsWC</span></a></span><span> </span><span class="annot"><span class="annottext">[WantedConstraints]
</span><a href="#local-6989586621681174156"><span class="hs-identifier hs-var">wanteds</span></a></span><span>
</span><span id="line-801"></span><span>
</span><span id="line-802"></span><span>       </span><span class="hs-comment">-- It's not yet zonked!  Obviously zonk it before peering at it</span><span>
</span><span id="line-803"></span><span>       </span><span class="hs-special">;</span><span> </span><span id="local-6989586621681174149"><span class="annot"><span class="annottext">WantedConstraints
</span><a href="#local-6989586621681174149"><span class="hs-identifier hs-var">solved_wanteds</span></a></span></span><span> </span><span class="hs-glyph">&lt;-</span><span> </span><span class="annot"><span class="annottext">WantedConstraints -&gt; TcM WantedConstraints
</span><a href="GHC.Tc.Utils.TcMType.html#zonkWC"><span class="hs-identifier hs-var">zonkWC</span></a></span><span> </span><span class="annot"><span class="annottext">WantedConstraints
</span><a href="#local-6989586621681174154"><span class="hs-identifier hs-var">solved_wanteds</span></a></span><span>
</span><span id="line-804"></span><span>
</span><span id="line-805"></span><span>       </span><span class="hs-comment">-- See [STEP DAC HOIST]</span><span>
</span><span id="line-806"></span><span>       </span><span class="hs-comment">-- From the simplified constraints extract a subset 'good' that will</span><span>
</span><span id="line-807"></span><span>       </span><span class="hs-comment">-- become the context 'min_theta' for the derived instance.</span><span>
</span><span id="line-808"></span><span>       </span><span class="hs-special">;</span><span> </span><span class="hs-keyword">let</span><span> </span><span id="local-6989586621681174147"><span class="annot"><span class="annottext">residual_simple :: Cts
</span><a href="#local-6989586621681174147"><span class="hs-identifier hs-var hs-var">residual_simple</span></a></span></span><span> </span><span class="hs-glyph">=</span><span> </span><span class="annot"><span class="annottext">Bool -&gt; WantedConstraints -&gt; Cts
</span><a href="GHC.Tc.Solver.html#approximateWC"><span class="hs-identifier hs-var">approximateWC</span></a></span><span> </span><span class="annot"><span class="annottext">Bool
</span><span class="hs-identifier hs-var">True</span></span><span> </span><span class="annot"><span class="annottext">WantedConstraints
</span><a href="#local-6989586621681174149"><span class="hs-identifier hs-var">solved_wanteds</span></a></span><span>
</span><span id="line-809"></span><span>             </span><span id="local-6989586621681174145"><span class="annot"><span class="annottext">good :: Bag PredType
</span><a href="#local-6989586621681174145"><span class="hs-identifier hs-var hs-var">good</span></a></span></span><span> </span><span class="hs-glyph">=</span><span> </span><span class="annot"><span class="annottext">(Ct -&gt; Maybe PredType) -&gt; Cts -&gt; Bag PredType
forall a b. (a -&gt; Maybe b) -&gt; Bag a -&gt; Bag b
</span><a href="GHC.Data.Bag.html#mapMaybeBag"><span class="hs-identifier hs-var">mapMaybeBag</span></a></span><span> </span><span class="annot"><span class="annottext">Ct -&gt; Maybe PredType
</span><a href="#local-6989586621681174143"><span class="hs-identifier hs-var">get_good</span></a></span><span> </span><span class="annot"><span class="annottext">Cts
</span><a href="#local-6989586621681174147"><span class="hs-identifier hs-var">residual_simple</span></a></span><span>
</span><span id="line-810"></span><span>
</span><span id="line-811"></span><span>             </span><span class="hs-comment">-- Returns @Just p@ (where @p@ is the type of the Ct) if a Ct is</span><span>
</span><span id="line-812"></span><span>             </span><span class="hs-comment">-- suitable to be inferred in the context of a derived instance.</span><span>
</span><span id="line-813"></span><span>             </span><span class="hs-comment">-- Returns @Nothing@ if the Ct is too exotic.</span><span>
</span><span id="line-814"></span><span>             </span><span class="hs-comment">-- See Note [Exotic derived instance contexts] for what</span><span>
</span><span id="line-815"></span><span>             </span><span class="hs-comment">-- constitutes an exotic constraint.</span><span>
</span><span id="line-816"></span><span>             </span><span class="annot"><a href="#local-6989586621681174143"><span class="hs-identifier hs-type">get_good</span></a></span><span> </span><span class="hs-glyph">::</span><span> </span><span class="annot"><a href="GHC.Tc.Types.Constraint.html#Ct"><span class="hs-identifier hs-type">Ct</span></a></span><span> </span><span class="hs-glyph">-&gt;</span><span> </span><span class="annot"><a href="../../base/src/GHC.Maybe.html#Maybe"><span class="hs-identifier hs-type">Maybe</span></a></span><span> </span><span class="annot"><a href="GHC.Core.TyCo.Rep.html#PredType"><span class="hs-identifier hs-type">PredType</span></a></span><span>
</span><span id="line-817"></span><span>             </span><span id="local-6989586621681174143"><span class="annot"><span class="annottext">get_good :: Ct -&gt; Maybe PredType
</span><a href="#local-6989586621681174143"><span class="hs-identifier hs-var hs-var">get_good</span></a></span></span><span> </span><span id="local-6989586621681174142"><span class="annot"><span class="annottext">Ct
</span><a href="#local-6989586621681174142"><span class="hs-identifier hs-var">ct</span></a></span></span><span> </span><span class="hs-glyph">|</span><span> </span><span class="annot"><span class="annottext">VarSet -&gt; PredType -&gt; Bool
</span><a href="GHC.Tc.Validity.html#validDerivPred"><span class="hs-identifier hs-var">validDerivPred</span></a></span><span> </span><span class="annot"><span class="annottext">VarSet
</span><a href="#local-6989586621681174206"><span class="hs-identifier hs-var">skol_set</span></a></span><span> </span><span class="annot"><span class="annottext">PredType
</span><a href="#local-6989586621681174141"><span class="hs-identifier hs-var">p</span></a></span><span>
</span><span id="line-818"></span><span>                         </span><span class="hs-special">,</span><span> </span><span class="annot"><span class="annottext">Ct -&gt; Bool
</span><a href="GHC.Tc.Types.Constraint.html#isWantedCt"><span class="hs-identifier hs-var">isWantedCt</span></a></span><span> </span><span class="annot"><span class="annottext">Ct
</span><a href="#local-6989586621681174142"><span class="hs-identifier hs-var">ct</span></a></span><span>
</span><span id="line-819"></span><span>                         </span><span class="hs-glyph">=</span><span> </span><span class="annot"><span class="annottext">PredType -&gt; Maybe PredType
forall a. a -&gt; Maybe a
</span><a href="../../base/src/GHC.Maybe.html#Just"><span class="hs-identifier hs-var">Just</span></a></span><span> </span><span class="annot"><span class="annottext">PredType
</span><a href="#local-6989586621681174141"><span class="hs-identifier hs-var">p</span></a></span><span>
</span><span id="line-820"></span><span>                          </span><span class="hs-comment">-- TODO: This is wrong</span><span>
</span><span id="line-821"></span><span>                          </span><span class="hs-comment">-- NB re 'isWantedCt': residual_wanted may contain</span><span>
</span><span id="line-822"></span><span>                          </span><span class="hs-comment">-- unsolved CtDerived and we stick them into the</span><span>
</span><span id="line-823"></span><span>                          </span><span class="hs-comment">-- bad set so that reportUnsolved may decide what</span><span>
</span><span id="line-824"></span><span>                          </span><span class="hs-comment">-- to do with them</span><span>
</span><span id="line-825"></span><span>                         </span><span class="hs-glyph">|</span><span> </span><span class="annot"><span class="annottext">Bool
</span><a href="../../base/src/GHC.Base.html#otherwise"><span class="hs-identifier hs-var">otherwise</span></a></span><span>
</span><span id="line-826"></span><span>                         </span><span class="hs-glyph">=</span><span> </span><span class="annot"><span class="annottext">Maybe PredType
forall a. Maybe a
</span><a href="../../base/src/GHC.Maybe.html#Nothing"><span class="hs-identifier hs-var">Nothing</span></a></span><span>
</span><span id="line-827"></span><span>                           </span><span class="hs-keyword">where</span><span> </span><span id="local-6989586621681174141"><span class="annot"><span class="annottext">p :: PredType
</span><a href="#local-6989586621681174141"><span class="hs-identifier hs-var hs-var">p</span></a></span></span><span> </span><span class="hs-glyph">=</span><span> </span><span class="annot"><span class="annottext">Ct -&gt; PredType
</span><a href="GHC.Tc.Types.Constraint.html#ctPred"><span class="hs-identifier hs-var">ctPred</span></a></span><span> </span><span class="annot"><span class="annottext">Ct
</span><a href="#local-6989586621681174142"><span class="hs-identifier hs-var">ct</span></a></span><span>
</span><span id="line-828"></span><span>
</span><span id="line-829"></span><span>       </span><span class="hs-special">;</span><span> </span><span class="annot"><span class="annottext">String -&gt; SDoc -&gt; IOEnv (Env TcGblEnv TcLclEnv) ()
</span><a href="GHC.Tc.Utils.Monad.html#traceTc"><span class="hs-identifier hs-var">traceTc</span></a></span><span> </span><span class="annot"><span class="annottext">String
</span><span class="hs-string">&quot;simplifyDeriv outputs&quot;</span></span><span> </span><span class="annot"><span class="annottext">(SDoc -&gt; IOEnv (Env TcGblEnv TcLclEnv) ())
-&gt; SDoc -&gt; IOEnv (Env TcGblEnv TcLclEnv) ()
forall a b. (a -&gt; b) -&gt; a -&gt; b
</span><a href="../../base/src/GHC.Base.html#%24"><span class="hs-operator hs-var">$</span></a></span><span>
</span><span id="line-830"></span><span>         </span><span class="annot"><span class="annottext">[SDoc] -&gt; SDoc
</span><a href="GHC.Utils.Outputable.html#vcat"><span class="hs-identifier hs-var">vcat</span></a></span><span> </span><span class="hs-special">[</span><span> </span><span class="annot"><span class="annottext">[TcTyVar] -&gt; SDoc
forall a. Outputable a =&gt; a -&gt; SDoc
</span><a href="GHC.Utils.Outputable.html#ppr"><span class="hs-identifier hs-var">ppr</span></a></span><span> </span><span class="annot"><span class="annottext">[TcTyVar]
</span><a href="#local-6989586621681174208"><span class="hs-identifier hs-var">tvs_skols</span></a></span><span class="hs-special">,</span><span> </span><span class="annot"><span class="annottext">Cts -&gt; SDoc
forall a. Outputable a =&gt; a -&gt; SDoc
</span><a href="GHC.Utils.Outputable.html#ppr"><span class="hs-identifier hs-var">ppr</span></a></span><span> </span><span class="annot"><span class="annottext">Cts
</span><a href="#local-6989586621681174147"><span class="hs-identifier hs-var">residual_simple</span></a></span><span class="hs-special">,</span><span> </span><span class="annot"><span class="annottext">Bag PredType -&gt; SDoc
forall a. Outputable a =&gt; a -&gt; SDoc
</span><a href="GHC.Utils.Outputable.html#ppr"><span class="hs-identifier hs-var">ppr</span></a></span><span> </span><span class="annot"><span class="annottext">Bag PredType
</span><a href="#local-6989586621681174145"><span class="hs-identifier hs-var">good</span></a></span><span> </span><span class="hs-special">]</span><span>
</span><span id="line-831"></span><span>
</span><span id="line-832"></span><span>       </span><span class="hs-comment">-- Return the good unsolved constraints (unskolemizing on the way out.)</span><span>
</span><span id="line-833"></span><span>       </span><span class="hs-special">;</span><span> </span><span class="hs-keyword">let</span><span> </span><span id="local-6989586621681174138"><span class="annot"><span class="annottext">min_theta :: ThetaType
</span><a href="#local-6989586621681174138"><span class="hs-identifier hs-var hs-var">min_theta</span></a></span></span><span> </span><span class="hs-glyph">=</span><span> </span><span class="annot"><span class="annottext">(PredType -&gt; PredType) -&gt; ThetaType -&gt; ThetaType
forall a. (a -&gt; PredType) -&gt; [a] -&gt; [a]
</span><a href="GHC.Tc.Utils.TcType.html#mkMinimalBySCs"><span class="hs-identifier hs-var">mkMinimalBySCs</span></a></span><span> </span><span class="annot"><span class="annottext">PredType -&gt; PredType
forall a. a -&gt; a
</span><a href="../../base/src/GHC.Base.html#id"><span class="hs-identifier hs-var">id</span></a></span><span> </span><span class="hs-special">(</span><span class="annot"><span class="annottext">Bag PredType -&gt; ThetaType
forall a. Bag a -&gt; [a]
</span><a href="GHC.Data.Bag.html#bagToList"><span class="hs-identifier hs-var">bagToList</span></a></span><span> </span><span class="annot"><span class="annottext">Bag PredType
</span><a href="#local-6989586621681174145"><span class="hs-identifier hs-var">good</span></a></span><span class="hs-special">)</span><span>
</span><span id="line-834"></span><span>             </span><span class="hs-comment">-- An important property of mkMinimalBySCs (used above) is that in</span><span>
</span><span id="line-835"></span><span>             </span><span class="hs-comment">-- addition to removing constraints that are made redundant by</span><span>
</span><span id="line-836"></span><span>             </span><span class="hs-comment">-- superclass relationships, it also removes _duplicate_</span><span>
</span><span id="line-837"></span><span>             </span><span class="hs-comment">-- constraints.</span><span>
</span><span id="line-838"></span><span>             </span><span class="hs-comment">-- See Note [Gathering and simplifying constraints for</span><span>
</span><span id="line-839"></span><span>             </span><span class="hs-comment">--           DeriveAnyClass]</span><span>
</span><span id="line-840"></span><span>             </span><span id="local-6989586621681174133"><span class="annot"><span class="annottext">subst_skol :: TCvSubst
</span><a href="#local-6989586621681174133"><span class="hs-identifier hs-var hs-var">subst_skol</span></a></span></span><span> </span><span class="hs-glyph">=</span><span> </span><span class="annot"><span class="annottext">[TcTyVar] -&gt; ThetaType -&gt; TCvSubst
HasDebugCallStack =&gt; [TcTyVar] -&gt; ThetaType -&gt; TCvSubst
</span><a href="GHC.Core.TyCo.Subst.html#zipTvSubst"><span class="hs-identifier hs-var">zipTvSubst</span></a></span><span> </span><span class="annot"><span class="annottext">[TcTyVar]
</span><a href="#local-6989586621681174208"><span class="hs-identifier hs-var">tvs_skols</span></a></span><span> </span><span class="annot"><span class="annottext">(ThetaType -&gt; TCvSubst) -&gt; ThetaType -&gt; TCvSubst
forall a b. (a -&gt; b) -&gt; a -&gt; b
</span><a href="../../base/src/GHC.Base.html#%24"><span class="hs-operator hs-var">$</span></a></span><span> </span><span class="annot"><span class="annottext">[TcTyVar] -&gt; ThetaType
</span><a href="GHC.Core.TyCo.Rep.html#mkTyVarTys"><span class="hs-identifier hs-var">mkTyVarTys</span></a></span><span> </span><span class="annot"><span class="annottext">[TcTyVar]
</span><a href="#local-6989586621681174211"><span class="hs-identifier hs-var">tvs</span></a></span><span>
</span><span id="line-841"></span><span>                          </span><span class="hs-comment">-- The reverse substitution (sigh)</span><span>
</span><span id="line-842"></span><span>
</span><span id="line-843"></span><span>       </span><span class="hs-comment">-- See [STEP DAC RESIDUAL]</span><span>
</span><span id="line-844"></span><span>       </span><span class="hs-comment">-- Ensure that min_theta is enough to solve /all/ the constraints in</span><span>
</span><span id="line-845"></span><span>       </span><span class="hs-comment">-- solved_wanteds, by solving the implication constraint</span><span>
</span><span id="line-846"></span><span>       </span><span class="hs-comment">--</span><span>
</span><span id="line-847"></span><span>       </span><span class="hs-comment">--    forall tvs. min_theta =&gt; solved_wanteds</span><span>
</span><span id="line-848"></span><span>       </span><span class="hs-special">;</span><span> </span><span id="local-6989586621681174131"><span class="annot"><span class="annottext">[TcTyVar]
</span><a href="#local-6989586621681174131"><span class="hs-identifier hs-var">min_theta_vars</span></a></span></span><span> </span><span class="hs-glyph">&lt;-</span><span> </span><span class="annot"><span class="annottext">(PredType -&gt; TcM TcTyVar)
-&gt; ThetaType -&gt; IOEnv (Env TcGblEnv TcLclEnv) [TcTyVar]
forall (t :: * -&gt; *) (m :: * -&gt; *) a b.
(Traversable t, Monad m) =&gt;
(a -&gt; m b) -&gt; t a -&gt; m (t b)
</span><a href="../../base/src/Data.Traversable.html#mapM"><span class="hs-identifier hs-var">mapM</span></a></span><span> </span><span class="annot"><span class="annottext">PredType -&gt; TcM TcTyVar
forall gbl lcl. PredType -&gt; TcRnIf gbl lcl TcTyVar
</span><a href="GHC.Tc.Utils.TcMType.html#newEvVar"><span class="hs-identifier hs-var">newEvVar</span></a></span><span> </span><span class="annot"><span class="annottext">ThetaType
</span><a href="#local-6989586621681174138"><span class="hs-identifier hs-var">min_theta</span></a></span><span>
</span><span id="line-849"></span><span>       </span><span class="hs-special">;</span><span> </span><span class="hs-special">(</span><span id="local-6989586621681174130"><span class="annot"><span class="annottext">Bag Implication
</span><a href="#local-6989586621681174130"><span class="hs-identifier hs-var">leftover_implic</span></a></span></span><span class="hs-special">,</span><span> </span><span class="annot"><span class="annottext">TcEvBinds
</span><span class="hs-identifier">_</span></span><span class="hs-special">)</span><span>
</span><span id="line-850"></span><span>           </span><span class="hs-glyph">&lt;-</span><span> </span><span class="annot"><span class="annottext">TcLevel
-&gt; SkolemInfo
-&gt; [TcTyVar]
-&gt; [TcTyVar]
-&gt; WantedConstraints
-&gt; TcM (Bag Implication, TcEvBinds)
</span><a href="GHC.Tc.Utils.Unify.html#buildImplicationFor"><span class="hs-identifier hs-var">buildImplicationFor</span></a></span><span> </span><span class="annot"><span class="annottext">TcLevel
</span><a href="#local-6989586621681174157"><span class="hs-identifier hs-var">tc_lvl</span></a></span><span> </span><span class="annot"><span class="annottext">SkolemInfo
</span><a href="#local-6989586621681174204"><span class="hs-identifier hs-var">skol_info</span></a></span><span> </span><span class="annot"><span class="annottext">[TcTyVar]
</span><a href="#local-6989586621681174208"><span class="hs-identifier hs-var">tvs_skols</span></a></span><span>
</span><span id="line-851"></span><span>                                  </span><span class="annot"><span class="annottext">[TcTyVar]
</span><a href="#local-6989586621681174131"><span class="hs-identifier hs-var">min_theta_vars</span></a></span><span> </span><span class="annot"><span class="annottext">WantedConstraints
</span><a href="#local-6989586621681174149"><span class="hs-identifier hs-var">solved_wanteds</span></a></span><span>
</span><span id="line-852"></span><span>       </span><span class="hs-comment">-- This call to simplifyTop is purely for error reporting</span><span>
</span><span id="line-853"></span><span>       </span><span class="hs-comment">-- See Note [Error reporting for deriving clauses]</span><span>
</span><span id="line-854"></span><span>       </span><span class="hs-comment">-- See also Note [Exotic derived instance contexts], which are caught</span><span>
</span><span id="line-855"></span><span>       </span><span class="hs-comment">-- in this line of code.</span><span>
</span><span id="line-856"></span><span>       </span><span class="hs-special">;</span><span> </span><span class="annot"><span class="annottext">Bag Implication -&gt; IOEnv (Env TcGblEnv TcLclEnv) ()
</span><a href="GHC.Tc.Solver.html#simplifyTopImplic"><span class="hs-identifier hs-var">simplifyTopImplic</span></a></span><span> </span><span class="annot"><span class="annottext">Bag Implication
</span><a href="#local-6989586621681174130"><span class="hs-identifier hs-var">leftover_implic</span></a></span><span>
</span><span id="line-857"></span><span>
</span><span id="line-858"></span><span>       </span><span class="hs-special">;</span><span> </span><span class="annot"><span class="annottext">ThetaType -&gt; IOEnv (Env TcGblEnv TcLclEnv) ThetaType
forall (m :: * -&gt; *) a. Monad m =&gt; a -&gt; m a
</span><a href="../../base/src/GHC.Base.html#return"><span class="hs-identifier hs-var">return</span></a></span><span> </span><span class="hs-special">(</span><span class="annot"><span class="annottext">HasCallStack =&gt; TCvSubst -&gt; ThetaType -&gt; ThetaType
TCvSubst -&gt; ThetaType -&gt; ThetaType
</span><a href="GHC.Core.TyCo.Subst.html#substTheta"><span class="hs-identifier hs-var">substTheta</span></a></span><span> </span><span class="annot"><span class="annottext">TCvSubst
</span><a href="#local-6989586621681174133"><span class="hs-identifier hs-var">subst_skol</span></a></span><span> </span><span class="annot"><span class="annottext">ThetaType
</span><a href="#local-6989586621681174138"><span class="hs-identifier hs-var">min_theta</span></a></span><span class="hs-special">)</span><span> </span><span class="hs-special">}</span><span>
</span><span id="line-859"></span><span>
</span><span id="line-860"></span><span class="hs-comment">{-
Note [Overlap and deriving]
~~~~~~~~~~~~~~~~~~~~~~~~~~~
Consider some overlapping instances:
  instance Show a =&gt; Show [a] where ..
  instance Show [Char] where ...

Now a data type with deriving:
  data T a = MkT [a] deriving( Show )

We want to get the derived instance
  instance Show [a] =&gt; Show (T a) where...
and NOT
  instance Show a =&gt; Show (T a) where...
so that the (Show (T Char)) instance does the Right Thing

It's very like the situation when we're inferring the type
of a function
   f x = show [x]
and we want to infer
   f :: Show [a] =&gt; a -&gt; String

BOTTOM LINE: use vanilla, non-overlappable skolems when inferring
             the context for the derived instance.
             Hence tcInstSkolTyVars not tcInstSuperSkolTyVars

Note [Gathering and simplifying constraints for DeriveAnyClass]
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
DeriveAnyClass works quite differently from stock and newtype deriving in
the way it gathers and simplifies constraints to be used in a derived
instance's context. Stock and newtype deriving gather constraints by looking
at the data constructors of the data type for which we are deriving an
instance. But DeriveAnyClass doesn't need to know about a data type's
definition at all!

To see why, consider this example of DeriveAnyClass:

  class Foo a where
    bar :: forall b. Ix b =&gt; a -&gt; b -&gt; String
    default bar :: (Show a, Ix c) =&gt; a -&gt; c -&gt; String
    bar x y = show x ++ show (range (y,y))

    baz :: Eq a =&gt; a -&gt; a -&gt; Bool
    default baz :: (Ord a, Show a) =&gt; a -&gt; a -&gt; Bool
    baz x y = compare x y == EQ

Because 'bar' and 'baz' have default signatures, this generates a top-level
definition for these generic default methods

  $gdm_bar :: forall a. Foo a
           =&gt; forall c. (Show a, Ix c)
           =&gt; a -&gt; c -&gt; String
  $gdm_bar x y = show x ++ show (range (y,y))

(and similarly for baz).  Now consider a 'deriving' clause
  data Maybe s = ... deriving Foo

This derives an instance of the form:
  instance (CX) =&gt; Foo (Maybe s) where
    bar = $gdm_bar
    baz = $gdm_baz

Now it is GHC's job to fill in a suitable instance context (CX).  If
GHC were typechecking the binding
   bar = $gdm bar
it would
   * skolemise the expected type of bar
   * instantiate the type of $gdm_bar with meta-type variables
   * build an implication constraint

[STEP DAC BUILD]
So that's what we do.  We build the constraint (call it C1)

   forall[2] b. Ix b =&gt; (Show (Maybe s), Ix cc,
                        Maybe s -&gt; b -&gt; String
                            ~ Maybe s -&gt; cc -&gt; String)

Here:
* The level of this forall constraint is forall[2], because we are later
  going to wrap it in a forall[1] in [STEP DAC RESIDUAL]

* The 'b' comes from the quantified type variable in the expected type
  of bar (i.e., 'to_anyclass_skols' in 'ThetaOrigin'). The 'cc' is a unification
  variable that comes from instantiating the quantified type variable 'c' in
  $gdm_bar's type (i.e., 'to_anyclass_metas' in 'ThetaOrigin).

* The (Ix b) constraint comes from the context of bar's type
  (i.e., 'to_wanted_givens' in 'ThetaOrigin'). The (Show (Maybe s)) and (Ix cc)
  constraints come from the context of $gdm_bar's type
  (i.e., 'to_anyclass_givens' in 'ThetaOrigin').

* The equality constraint (Maybe s -&gt; b -&gt; String) ~ (Maybe s -&gt; cc -&gt; String)
  comes from marrying up the instantiated type of $gdm_bar with the specified
  type of bar. Notice that the type variables from the instance, 's' in this
  case, are global to this constraint.

Note that it is vital that we instantiate the `c` in $gdm_bar's type with a new
unification variable for each iteration of simplifyDeriv. If we re-use the same
unification variable across multiple iterations, then bad things can happen,
such as #14933.

Similarly for 'baz', giving the constraint C2

   forall[2]. Eq (Maybe s) =&gt; (Ord a, Show a,
                              Maybe s -&gt; Maybe s -&gt; Bool
                                ~ Maybe s -&gt; Maybe s -&gt; Bool)

In this case baz has no local quantification, so the implication
constraint has no local skolems and there are no unification
variables.

[STEP DAC SOLVE]
We can combine these two implication constraints into a single
constraint (C1, C2), and simplify, unifying cc:=b, to get:

   forall[2] b. Ix b =&gt; Show a
   /\
   forall[2]. Eq (Maybe s) =&gt; (Ord a, Show a)

[STEP DAC HOIST]
Let's call that (C1', C2').  Now we need to hoist the unsolved
constraints out of the implications to become our candidate for
(CX). That is done by approximateWC, which will return:

  (Show a, Ord a, Show a)

Now we can use mkMinimalBySCs to remove superclasses and duplicates, giving

  (Show a, Ord a)

And that's what GHC uses for CX.

[STEP DAC RESIDUAL]
In this case we have solved all the leftover constraints, but what if
we don't?  Simple!  We just form the final residual constraint

   forall[1] s. CX =&gt; (C1',C2')

and simplify that. In simple cases it'll succeed easily, because CX
literally contains the constraints in C1', C2', but if there is anything
more complicated it will be reported in a civilised way.

Note [Error reporting for deriving clauses]
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
A surprisingly tricky aspect of deriving to get right is reporting sensible
error messages. In particular, if simplifyDeriv reaches a constraint that it
cannot solve, which might include:

1. Insoluble constraints
2. &quot;Exotic&quot; constraints (See Note [Exotic derived instance contexts])

Then we report an error immediately in simplifyDeriv.

Another possible choice is to punt and let another part of the typechecker
(e.g., simplifyInstanceContexts) catch the errors. But this tends to lead
to worse error messages, so we do it directly in simplifyDeriv.

simplifyDeriv checks for errors in a clever way. If the deriving machinery
infers the context (Foo a)--that is, if this instance is to be generated:

  instance Foo a =&gt; ...

Then we form an implication of the form:

  forall a. Foo a =&gt; &lt;residual_wanted_constraints&gt;

And pass it to the simplifier. If the context (Foo a) is enough to discharge
all the constraints in &lt;residual_wanted_constraints&gt;, then everything is
hunky-dory. But if &lt;residual_wanted_constraints&gt; contains, say, an insoluble
constraint, then (Foo a) won't be able to solve it, causing GHC to error.

Note [Exotic derived instance contexts]
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
In a 'derived' instance declaration, we *infer* the context.  It's a
bit unclear what rules we should apply for this; the Haskell report is
silent.  Obviously, constraints like (Eq a) are fine, but what about
        data T f a = MkT (f a) deriving( Eq )
where we'd get an Eq (f a) constraint.  That's probably fine too.

One could go further: consider
        data T a b c = MkT (Foo a b c) deriving( Eq )
        instance (C Int a, Eq b, Eq c) =&gt; Eq (Foo a b c)

Notice that this instance (just) satisfies the Paterson termination
conditions.  Then we *could* derive an instance decl like this:

        instance (C Int a, Eq b, Eq c) =&gt; Eq (T a b c)
even though there is no instance for (C Int a), because there just
*might* be an instance for, say, (C Int Bool) at a site where we
need the equality instance for T's.

However, this seems pretty exotic, and it's quite tricky to allow
this, and yet give sensible error messages in the (much more common)
case where we really want that instance decl for C.

So for now we simply require that the derived instance context
should have only type-variable constraints.

Here is another example:
        data Fix f = In (f (Fix f)) deriving( Eq )
Here, if we are prepared to allow -XUndecidableInstances we
could derive the instance
        instance Eq (f (Fix f)) =&gt; Eq (Fix f)
but this is so delicate that I don't think it should happen inside
'deriving'. If you want this, write it yourself!

NB: if you want to lift this condition, make sure you still meet the
termination conditions!  If not, the deriving mechanism generates
larger and larger constraints.  Example:
  data Succ a = S a
  data Seq a = Cons a (Seq (Succ a)) | Nil deriving Show

Note the lack of a Show instance for Succ.  First we'll generate
  instance (Show (Succ a), Show a) =&gt; Show (Seq a)
and then
  instance (Show (Succ (Succ a)), Show (Succ a), Show a) =&gt; Show (Seq a)
and so on.  Instead we want to complain of no instance for (Show (Succ a)).

The bottom line
~~~~~~~~~~~~~~~
Allow constraints which consist only of type variables, with no repeats.
-}</span><span>
</span><span id="line-1082"></span></pre></body></html>